Open In App

How to Append Text to End of File in Linux?

Last Updated : 07 Mar, 2021
Improve
Improve
Like Article
Like
Save
Share
Report

On Linux, while working with files in a terminal sometimes we need to append the same data of a command output or file content. Append means simply add the data to the file without erasing existing data. Today we are going to see how can we append the text in the file on the terminal.

Using >> Operator:

The >> operator redirects output to a file. If the mentioned file doesn’t exist the file is created and then the text is appended to the file.

Examples:

We can use the echo command to append the text to the file, like

echo "Which is the best Linux Distro?"  >>  file.txt

How to Append Text to End of File in Linux

Alternatively, we can use printf command to append text into a file.

printf "Which is the best Linux Distro?\n"  >>  file.txt

How to Append Text to End of File in Linux

We can also use the cat command to append the content of one file to another file.

Example:

In this example, we have to append the content of file file1.txt to the file file2.txt

cat file1.txt >> file2.txt

How to Append Text to End of File in Linux

Note: Don’t use > of >> This will erase the data of the target file. This may cause data loss.

Using tee Command:

The tee command copies the text from standard input and writes it to the standard output file. The tee provides -a option to append the text to the file.

echo "Which is the best Linux Distro?"   | tee -a file.txt

How to Append Text to End of File in Linux

We can also append the content of a file into another file using the tee command

Example:

In this example, we have to append the content of file file1.txt to the file file2.txt

cat file1.txt | tee -a file2.txt

How to Append Text to End of File in Linux

This is how we append the text into a file in Linux.


Like Article
Suggest improvement
Previous
Next
Share your thoughts in the comments

Similar Reads