In Linux, we append stuff to text files all the time. Say we have an options file, with a key/value pair on each line. If we want to quickly append a new option, it can take just one line instead of taking the time to open the file in a text editor, scroll down, make the changes and saving it. In this tutorial, we’ll look at a couple of different ways to append text to files in Linux using I/O redirection as well as the “sed” command.
Appending using I/O Redirection
1. Using cat:
Linux allows us to redirect a stream of text into a file and append it. The “cat” command is the most commonly used tool and it’s also the most elegant. Let’s say we have a file called “existingfile” with a single line of text. To append another line of text to it, we can use:
cat >> existingfile [Add text here] ctrl+d
After executing the first line, you add whatever text you want in the [Add text here] area. You can use as many lines as you want. This allows you to append paragraphs, use special characters etc. When you’re done, type “ctrl+d” to signify the end of file (EOF) character. This will exit the command and save the file with whatever text you appended to it. Here’s an example of how it works:
In this example, I’ve appended the text “Adding another line here” to “existingfile”. Don’t forget to use double arrows “>>”. If you use a single arrow by mistake, you’ll end up overwriting the file instead of appending!
2. Using echo:
Another quick way to append text to a file is to simply redirect the output of an “echo” command. Like this:
echo "Adding another line with echo" >> existingfile
Here’s how it works:
Note that unlike cat, you can’t use “echo” to input multiple lines of text easily since as soon as the press the “Enter” key, it will execute. Moreover, there are some special characters that won’t work. For example, if you try and include an exclamation mark (!), the echo command will not work properly because that’s a reserved bash character. There are definitely work around with escape characters etc, but the bottom line is that “cat” is far more reliable than “echo” to append text to a file in Linux.
3. Using Single Arrow Symbols (>):
Note how in the above examples, we used double arrows (>>). This is very important. If you accidentally use a single arrow (>), you end up overwriting the file instead of appending to it! It’s actually a shockingly common error. Here’s an example:
In the above example, using single arrows (>) completely replaced existing file contents – both using the echo as well as the cat. This default behavior of bash can be modified by using the following command:
set -o noclobber
With this option set, using single arrow (>) symbols will cause a warning and result in an error. Like this:
I recommend you always keep this option on. It’s too risky otherwise!
Using non I/O Redirection Methods
The above techniques rely on I/O redirection. However, most Linux distributions come bundled with a tool called “sed” – short for “Stream Editor”. The tool has notoriously bad documentation, but you can append stuff to files using sed with the following command:
sed -i '$ a\[To be added]' existingfile
Replace [To be added] with whatever text you want to append. Here’s an example of sed at work:
The “$” character signifies end of line, the “-i” is for editing the file specified at the end, and “a” stands for “append”. As you can see it gets the job done, but is a bit clumsy to use. But if you need a non-I/O redirect solution for whatever reason, this is one way to do it.
And that pretty much sums up appending text quickly to a file in Linux. The “cat” command seems the easiest and cleanest, but there are other options for you if you want them!