Append Text to File in Linux

Append Text to File in Linux

When working with files in Linux, there might be needed to add text at the end of the file without deleting its content. This operation is known as text appending. This tutorial shows how to do that.

To append text to the end of a file without overwriting its content, we can use >> redirection operator:

echo "First line" >> test.txt
echo "Second line" >> test.txt

If file not exist, it will be created.

If need to append text to a file that requires sudo privileges, we can use tee command with -a option:

echo "127.0.0.1 test-app.local" | sudo tee -a /etc/hosts >/dev/null

After appending text to a file, text is shown in the terminal as well. To prevent this, output is redirected to a null device file >/dev/null.

Leave a Comment

Cancel reply

Your email address will not be published.