Bash: Append to File

Posted 

3 min read

In Bash, there are multiple ways to append text to a file. This article explains some of them.

To append text to a file, you need to have write permissions to it. Otherwise, you will receive a permission denied error.

Append to a File using the Redirection Operator (>>)

Redirection allows you to capture the output from a command and send it as input to another command or file. The >> redirection operator appends the output to a given file.

There are a number of commands that you can use to print text to the standard output and redirect it to the file, with echo and printf being the most used ones.

To append text to a file, specify the name of the file after the redirection operator:

echo "this is a new line" >> file.txt

When used with the -e option the echo command interprets the backslash-escaped characters such as newline \n:

echo -e "this is a new line \nthis is another new line" >> file.txt

To produce more complex output, use the printf command which allows you to specify the formatting of the output:

printf "Hello, I'm %s.\n" $USER >> file.txt

Another way to append text to a file is to use the Here document (Heredoc). It is a type of redirection that allows you to pass multiple lines of input to a command.

For example, you can pass the content to the cat command and append it to a file:

cat « EOF » file.txt The current working directory is: $PWD You are logged in as: $(whoami) EOF

You can append the output of any command to a file. Here is an example with the date command:

date +"Year: %Y, Month: %m, Day: %d" >> file.txt

When appending to a file using a redirection, be careful not to use the > operator to overwrite an important existing file.

Append to a File using the tee Command

tee is a command-line utility in Linux that reads from the standard input and writes to both standard output and one or more files at the same time.

By default, the tee command overwrites the specified file. To append the output to the file use tee with the -a (--append) option:

echo "this is a new line"  | tee -a file.txt

If you don’t want tee to write to the standard output, redirect it to /dev/null:

echo "this is a new line"  | tee -a file.txt >/dev/null

The advantage of using the tee command over the >> operator is that tee allows you to append text to multiple files at once, and to write to files owned by other users in conjunction with sudo.

To append text to a file that you don’t have write permissions to, prepend sudo before tee as shown below:

echo "this is a new line" | sudo tee -a file.txt

tee receives the output of the echo command, elevates the sudo permissions, and writes to the file.

To append text to more than one file, specify the files as arguments to the tee command:

echo "this is a new line"  | tee -a file1.txt file2.txt file3.txt

Conclusion

In Linux, to append text to a file, use the >> redirection operator or the tee command.

If you have any questions or feedback, feel free to leave a comment.