Working in the Linux terminal often involves logging data or updating configuration files. If you need to quickly add a single line of text to the absolute bottom of a file—perhaps adding a new IP address to a blocklist or recording a timestamp in a log—opening the file in a full-screen text editor like nano or vim is unnecessarily time-consuming.
Instead, Linux provides a powerful command-line shortcut called “redirection.” By using a specific mathematical operator, you can instantly inject a string of text directly into the bottom of any existing file without ever actually opening the file.
This guide explains how to safely use the >> append operator in the Ubuntu terminal.
The Double Greater-Than Operator (>>)
To write text from the command line into a file, you combine the echo command (which normally just prints text to the screen) with a redirection operator.
The standard redirection operator is a single greater-than sign (>). You must be incredibly careful not to use this. A single > tells Linux to overwrite the entire file. If you use it, all existing data in the file is instantly destroyed and replaced by your new text.
To safely add data to a file while preserving everything currently inside it, you must use the double greater-than sign (>>). This tells Linux to “append” the data to the very end of the file.
How to Append Text
Assume you have a file named notes.txt and you want to add a reminder to the bottom of it.
- Open your terminal.
- Type the word
echofollowed by the exact text you want to add, enclosed in quotation marks. - Add a space, then type the double greater-than operator (
>>). - Add another space, and type the name of the destination file.
The final command looks like this:
echo "Remember to restart the server at midnight" >> notes.txt
When you press Enter, the terminal will return to a blank prompt silently. The text has been added.
Verifying the Change
To prove that the text was added to the bottom of the file without destroying the previous contents, you can use the tail command, which prints the last few lines of a file to the screen.
tail -n 3 notes.txt
You will see your new sentence printed on a fresh line at the absolute bottom of the document.
Pro Tip: If you attempt to append text to a file that does not actually exist yet, Linux is smart enough to realize this. It will automatically create a brand new, empty file with that name and insert your text into it.