From the point of view of someone who’s not very experienced with Linux, it can seem to be a very niche scenario where you’d want to append something to a file. But as soon as you begin managing a server, you’ll see we have many reasons to add content to a file. As a result, Linux has various tools allowing us to do this, each of which corresponds to a different use case scenario. For example, some tools allow us to write to multiple files at the same time, other syntaxes let us check for write errors before we start appending.
To begin with, let’s look at the basic techniques for appending a file in Linux.
How to Append a File in Bash
There are a huge number of ways in which you can append content to a file in bash.
Basic “echo” Usage to Append
The most basic use case for appending a file in bash is like this:
echo "New line of text" >> filename.txt
The above command will append “New line of text” to “filename.txt. If the file doesn’t exist, bash will create it. If it’s not a text file, then the results could vary, but will most likely result in the corruption of the file. For example, if you try and append text to a PNG image file, you won’t be able to open the image properly.
Appending content to other file types can be potentially more dangerous. For example, writing a system device file can have unpredictable system-wide effects. Or you could write to a special location like /dev/shm, in which case your content will be available via RAM to other processes.
Appending Multiple Lines to a File
When you use the echo command to append to a file as above, you can’t press the “Enter” key to write multiple lines. The easiest way to add multiple lines is to use the HEREDOC construct as shown below:
cat <<EOF >> filename.txt
This is line 1
This is line 2
EOF
I’ve written before about the incredible utility of the HEREDOC construct. We can use it here to append multiple lines and not worry about escaping special characters. If you want to know more about HEREDOC, I suggest you check out the tutorial I linked above.
Appending the Output of Commands
Most often, you won’t have the exact text of what you want to append in advance. Instead, the content will come from another command. Here’s how you append the output of a command to a file in bash:
date >> log.txt
ls -l >> filelist.txt
The above command will append the output of the “date” and the “ls” command to the two specified files. Note that the syntax is the same as the one for the “echo” command because, in the latter situation, we’re doing the same thing – appending the output of “echo” and adding it to a file. So you can say that this is a more general case of the first one.
Appending to Multiple Files at Once
All the above examples only allow you to write to a single file at once. Sometimes, you might want to add the same text to a bunch of files at the same time. For this, we use the “tee” command:
echo "Logging to multiple files" | tee -a file1.txt file2.txt
The “tee” command also has other uses, which often makes it superior as an append tool, particularly when you want to see the output while appending the output at the same time. With tee, you can also append to write protected files using sudo. For example:
echo "Privileged entry" | sudo tee -a /root/protected.txt
For example, the following example will NOT work:
sudo echo "Privileged entry" >> /root/protected.txt
Here’s a screenshot:
As you can see, merely using “sudo” for the echo statement isn’t enough. With the “tee” command, however, you can use sudo and append content to a write-protected file.
Adding Newlines Before Appending
When you append some text to a file in bash, it doesn’t check if the previous line had a new line before it. This can sometimes lead to unexpected formatting. For example, if we have a file:
First line
Second line
Third line (no newline here)
Then appending something to this file will make it this:
First line
Second line
Third line (no newline here)New entry
As you can imagine, this is probably not the expected output. The new entry should logically be on a separate line, given the existing structure of the file. Normally, when bash appends text to a file, it automatically adds a new line to the end of it. So appending further lines works as intended.
If, however, the manner in which the previous lines were added didn’t add a new line to the end of the last line, then appending a line presents you with the problem shown above. If you have something like a log file, then you know that all the previous lines were also appended, so you don’t have to worry. But if you need to check, then you can use the following command to add a new line to the end of the file if one doesn’t already exist:
sed -i -e '$a\' filename.txt
The above command uses sed – a powerful stream editor developed in the 1970s but still widely in use today. In an earlier NameHero tutorial, I explained how to use sed to delete lines. The above command actually exploits a quirk in the way the shell interprets backslashes and quotes – just don’t think about it too hard.
Once you’ve run the above command, you can then append whatever text you want without worrying about newlines.
Appending Safely to a File
You won’t always have permission to append text to a file. If you expect to encounter such situations, you have two options. First, you can just go for it! If it fails, it fails and no harm is done. In any case, you’ll get an error message like “Permission denied” if the bash append command isn’t successful.
The second option is to test for an error before appending. For example, something like this:
if [ -w filename.txt ]; then
echo "Appending safely" >> filename.txt
else
echo "Error: Cannot write to file"
fi
The above script first checks to see if the file is writable, and only then does it proceed with appending the text. If not, it throws an error message. The problem is that this adds another layer of checks, and it’s not clear that there’s any benefit to simply going for it and accepting the failure.
The one exception is where you want the script to stop executing if the append fails. For this, we use:
#!/bin/bash
set -e # Stop script on any error
The above line, when included on a script, causes the script to immediately exit. This can prevent a cascade of errors if the following commands rely on the successful execution of previous ones. It can also prevent your logs from filling up with errors if the script keeps running.
It’s a design decision either way, and there’s no right or wrong answer.
Conclusion
As you can see, there are many ways of appending content to a file in bash. Each of them is useful under different circumstances, so you should be familiar with the differences between them. Sometimes you might need to combine it with commands like “tee” to work with protected files as well.

I’m a NameHero team member, and an expert on WordPress and web hosting. I’ve been in this industry since 2008. I’ve also developed apps on Android and have written extensive tutorials on managing Linux servers. You can contact me on my website WP-Tweaks.com!
Leave a Reply