How to Append to End of a File in Linux

append to end of a file linux

In this tutorial, we learn different ways to append text to the end of a file in Linux. You can achieve this using several methods in Linux, but the simplest one is to redirect the command output to the desired filename. Using the >> character you can output result of any command to a text file.

Other ways this can be achieved, is using Linux tools like tee, awk, and sed.

Redirect output of command or data to end of file

Every Unix-based operating system has a concept of “a default place for output to go”. Everyone calls it “standard output”, or “stdout”, pronounced standard out. Your shell (probably bash or zsh) is constantly watching that default output place. When your shell sees new output there, it prints it out on the screen so that you can see it.

We can redirect that output to a file using the >> operator.

The procedure is as follows:

Append text to end of file using echo command:

echo 'sample text line' >> filename.txt

Append command output to end of file:

command >> filename.txt

Adding lines to end of file

We can add text lines using this redirect character >> or we can write data and command output to a text file. Using this method the file will be created if it doesn't exist.

For example:

$ echo "sample line" >> test.txt
$ cat test.txt 
sample line

$ echo "sample line 2" >> test.txt
$ cat test.txt 
sample line
sample line 2

Adding command data output result to end of file

You can also add data or run command and append output to the desired file. In this example, we will use date for appending the current date to a file, uname command - which will print out kernel version of the Linux system we are using and lastly ls command which outputs the current directory structure and file list.
You can use any command that can output it's result to a terminal, which means almost all of the command line tools in Linux.

$ date >> test.txt 
$ cat test.txt 
sample line
sample line 2
Tue Jun 25 20:28:51 UTC 2019


$ uname -r >> test.txt 
$ cat test.txt 
sample line
sample line 2
Tue Jun 25 20:28:51 UTC 2019
3.13.0-170-generic


$ ls >> test.txt 
$ cat test.txt 
sample line
sample line 2
Tue Jun 25 20:28:51 UTC 2019
3.13.0-170-generic
test.txt

Alternative methods

Lets see how to append using tee, awk and sed Linux utility.

Using tee command line tool

Tee command reads the standard input and writes it to both the standard output and one or more files. The command is named after the T-splitter used in plumbing. It breaks the output of a program so that it can be both displayed and saved in a file.

$ tee -a test.txt <<< "appended line of text"
[email protected]:~$ cat test.txt 
appended line of text

Using awk command line tool

Awk is a utility that enables a programmer to write tiny but effective programs in the form of statements that define text patterns that are to be searched for in each line of a document and the action that is to be taken when a match is found within a line. Awk is mostly used for pattern scanning and processing.

$ awk 'BEGIN{ printf "another text line appended" >> "test.txt"  }'
[email protected]:~$ cat test.txt 
another text line appended

Using sed command line tool

Sed command in Linux stands for stream editor and it can perform lots of functions on a file like searching, find and replace, insertion or deletion. By using sed you can edit files even without opening it, which is a much quicker way to find and replace something in the file.

$ sed -i '$a yet another text line' test.txt
[email protected]:~$ cat test.txt 
yet another text line

Append multiple lines to a file

There are several ways to append multiple lines to a file at once.
You can, of course, add lines one by one:

$ echo "line 1" >> result.txt
$ echo "line 2" >> result.txt

Next variant is to enter new line in terminal:

echo "line 1
line 2
line 3" >> result.txt

Another way is to open a file and write lines until you type EOT:

$ cat <<EOT >> result.txt
line 1
line 2
EOT

Conclusion

Although there are multiple ways to append the text lines or data and command output to a file, we saw that the easiest way is using >> redirect character. There are ways to append the text to an end of a specific line number in a file, or in the middle of a line using regex, but we will going to cover that in some other article.

Let us know what method to append to the end of the file you think is best down in the comments section.

Slavisa Milojkovic 8:50 am

Comments

Your email address will not be published. Required fields are marked *

All comments are subject to moderation.