Linux / Unix: Use Cat Command To Append Data To a File

I am a new Unix user. I have Debian Linux installed. I need to append text to a file called daily.log. How do I use the cat command to append data to a file?

You can use the cat command to append data or text to a file. The cat command can also append binary data. The main purpose of the cat command is to display data on screen (stdout) or concatenate files under Linux or Unix like operating systems. To append a single line you can use the echo or printf command.

Tutorial details
Difficulty level Easy
Root privileges No
Requirements cat on Linux or Unix
Est. reading time 1m

Redirection symbol


The syntax is:

cat file >> file2
echo 'text to append' >> file2
printf "text to append\n" >> fileName


Create a text file called foo.txt, type:

cat > foo.txt
This is a test.
I like Unix operating systems.

To save the changes press CTRL-d i.e. press and hold CTRL and press d. Create another text file called bar.txt as follows:

echo 'For desktop usage I prefer Apple OS X unix operating systems.' > bar.txt

Display both files on sceen, enter:

cat foo.txt
cat bar.txt

To append a contains of bar.txt to to foo.txt, enter:

cat bar.txt >> foo.txt
cat foo.txt

To append a ‘Use unix or die’ text to foo.txt file, enter:

echo 'Use unix or die' >> foo.txt
cat foo.txt

Sample outputs:

Fig.01: Using the cat and echo command to append a text to a file.

Fig.01: Using the cat and echo command to append a text to a file.

Append text to a file when using sudo command

We can use the echo command or printf command to append data to a file called sales.txt in the current directory:

echo 'some data' | sudo tee -a sales.txt
echo '1192.168.1.253 wireless-router' | sudo tee -a /etc/hosts

Want to append to a file? Try:
cat filename | sudo tee -a foo_file.txt
In this example append data using the following syntax:
sudo sh -c 'echo " wireless-router" >> /etc/hosts'
Verify it:
cat /etc/hosts

🐧 Get the latest tutorials on Linux, Open Source & DevOps via RSS feed or Weekly email newsletter.

🐧 10 comments so far... add one

CategoryList of Unix and Linux commands
Disk space analyzersncdu pydf
File Managementcat
FirewallAlpine Awall CentOS 8 OpenSUSE RHEL 8 Ubuntu 16.04 Ubuntu 18.04 Ubuntu 20.04
Network UtilitiesNetHogs dig host ip nmap
OpenVPNCentOS 7 CentOS 8 Debian 10 Debian 8/9 Ubuntu 18.04 Ubuntu 20.04
Package Managerapk apt
Processes Managementbg chroot cron disown fg jobs killall kill pidof pstree pwdx time
Searchinggrep whereis which
User Informationgroups id lastcomm last lid/libuser-lid logname members users whoami who w
WireGuard VPNAlpine CentOS 8 Debian 10 Firewall Ubuntu 20.04
10 comments… add one
  • sindhu Mar 8, 2014 @ 5:03

    when i execute two commands like echo $val(nn) > t.txt and awk -f throughput.awk >t.txt
    i want the file t.txt to have two columns side by side leaving a tab or space in between. but i m getting output like this


    pls help in this regard…thanks

    • 🐧 Nix Craft Mar 8, 2014 @ 6:19

      I’m not sure I understood your query. To merge corresponding or subsequent lines of files try paste command.

  • camden_kid Apr 23, 2014 @ 12:56


  • chiranjeevi Jun 18, 2014 @ 5:04


    plz help me for below req.

    I want to remove common numbers from two files.

    ex: In file1 numbers are 1,2,3,4 and In file2 are 1,2,3,4,5,6,7,8,9,10.

    I need a out like 5,6,7,8,9,10….
    Plz help me
    Thx in advances….

    • Nilabh Nov 12, 2014 @ 12:16

      Sed command will suffice your requirement.

  • kjell Sep 23, 2015 @ 13:15

    Cat text >> file
    does not work if text is a directory name. Pitty as this would be a nice way to collect usefull paths in a file

  • Tarikur Nov 25, 2015 @ 11:09

    sudo echo 'deb trusty-pgdg main 9.5' /etc/apt/sources.list.d/postgresql.list

    Not working in ubuntu. Its saying Permission denied

    • Sacheen Shah Jun 15, 2016 @ 15:35

      echo 'deb trusty-pgdg main 9.5' | sudo /etc/apt/sources.list.d/postgresql.list

  • Karthik Oct 6, 2016 @ 7:36

    I want to edit an existing file by adding few text in after particular content in an xml file. How can we do this.

  • ... Dec 6, 2017 @ 3:28


Leave a Reply

Your email address will not be published.

Use HTML <pre>...</pre> for code samples. Still have questions? Post it on our forum