Linux Commands

How to Add a Line to the End of a File in Linux

This Linux article describes different methods to append a line to the end of a file.

By reading this tutorial, you will be able to append new lines to the end of files both with and without superuser privileges in Linux using different techniques. All methods include examples.

The content is valid for all Linux distributions.

All instructions contain real screenshots to make it easy for any Linux user to understand and apply them.

Adding a line to the end of a file that doesn’t require superuser privileges

This section explains how to add a new line to the end of a file that does not require superuser privileges. After the following instructions, this tutorial shows how to add lines to files requiring privileges.

To begin, I created a file named linuxhintaddline. To see its content, I will use the less command as shown in the figure below.

less linuxhintaddline

As you can see below, the file contains 3 lines: “line 1”, “line 2”, and “line 3”.

The syntax to add a line to the end of the file is the following: “Line Content” is the text you want to add, and “File” is the file to which you want to add the line.

echo  'Line Content' >> File

Therefore, if I want to add the “line 4” to the linuxhintaddline file, I execute the command shown in the following screenshot:

echo  'line 4' >> linuxhintaddline

I want to check the file to confirm the line was properly appended.

less linuxhintaddline

As you can see in the image below, the fourth line was added successfully.

There are different ways to add lines. You also can use the printf command. The syntax is the following:

printf "Line Content" >> File

If I want to add a “line 5” last line to the linuxhintaddline file, I run the command shown below.

printf "line 5" >> linuxhintaddline

Now, just to use a different command, I will confirm if the line was added by executing the command below.

cat linuxhintaddline

The line was successfully added.

The following example describes how to add multiple lines to the end of a file.

How to a line to the end of a file requiring privileges

As said previously, the method above won’t affect files without superuser privileges. This section shows how to do it.

As you can see below, when trying to use the former command, I get an error because of a lack of permissions.

echo  'line 5' >> /root/linuxhintaddline

The syntax to append lines to files with root or sudo privileges is the following:

sudo sh -c "echo 'Line Content' >> /path/file"

For this example, I want to add the “line 5” text to the /root/linuxhintaddline file. To do it, I use the syntax explained above, as you can see in the figure below.

sudo sh -c "echo 'line 5' >> /root/linuxhintaddline"

Again, I use the less command to confirm the line was successfully added.

sudo less /root/linuxhintaddline

As you can see below, the line was properly added.

Another way to read the new line and write it into a file is by combining the echo command with tee through a pipe. The syntax is the following:

echo 'File Content' | sudo tee -a linuxhintaddline

Therefore, if I want to add the “line 6” line with privileges I type the command shown in the figure below.

echo 'line 6' | sudo tee -a /root/linuxhintaddline

To check if the line was properly added, I will run the less command again followed by the path.

sudo less /root/linuxhintaddline

And as you can see, the line was appended.

That’s how you can easily append lines to the end of files in Linux.

Conclusion:

Appending lines to the end of files is easy, and Linux flexibility allows more than one technique, even more than the one described in this document. As said previously, the above instructions are useful on all Linux distributions. It is recommended to write all commands by yourself rather than copying and pasting them.

About the author

David Adams

David Adams is a System Admin and writer that is focused on open source technologies, security software, and computer systems.