How to append to a text file in Linux?

Published on Aug. 22, 2023, 12:17 p.m.

You can append to a text file in Linux using the >> operator or the echo command. Here are some examples:

  1. Using >> operator:
echo "new line of text" >> file.txt

This will append the “new line of text” to the end of file.txt.
2. Using echo command with >> operator:

echo "another line of text" >> file.txt

This will append the “another line of text” to the end of file.txt.

Note that if the file doesn’t exist, the >> operator will create a new file. If the file already exists, it will append the text to the end of the file.

You can also use the printf command to append text to a file:

printf "new line of text\n" >> file.txt

This will append the “new line of text” to the end of file.txt, including a line break at the end of the text.

Additionally, you can also use the cat command to append the contents of one file to another:

cat file1.txt >> file2.txt

This will append the contents of file1.txt to the end of file2.txt.

Tags: