Skip to Content

2 ways to append text to the end of file in Linux

There are a few ways to append text to the end of a file in Linux. In this blog post, we will discuss two of them: using the “>>” operator, and using the “tee” command. Let’s get started!

Append text to the end of a file with >> operator in Linux

The “>>” operator is a simple way to append text to the end of a file. All you need to do is use the “>>” operator followed by the path of the file you want to append to. For example, if we wanted to append the text “This is some text” to the end of a file called “test.txt”, we would use the following command: echo “This is some text” >> test.txt

You can also use the “>>” operator to append the contents of one file to another file. For example, if we had a file called “textA.txt” that contained the text “This is some text”, and we wanted to append the contents of “textA.txt” to the end of “test.txt”, we would use the following command: cat textA.txt >> test.txt

Append text to the end of a file with tee command in Linux

The best way to append text to the end of a file is using the tee command in Linux. It can not only append text to a file, but it can also create a new file if one does not already exist. For example, if we wanted to append the text “This is some text” to the end of a file called “test.txt”, we would use the following command: echo “This is some text” |tee -a test.txt

Here are more examples of tee command.

  • # echo “/mnt/pg_master/wal_archives   10.20.20.5(rw,sync,no_root_squash)” | tee -a /etc/exports
  • # cat shares.txt | tee -a /etc/exports

 

As you can see, the “tee” command is a little more complex than the “>>” operator. However, it provides a lot more flexibility, which can be very useful in certain situations.

Both the “>>” operator and the “tee” command are useful ways to append text to a file in Linux. In most cases, either method will work just fine. However, if you need more flexibility, the “tee” command is the way to go. Thanks for reading! We hope this has been helpful.