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.
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.
Therefore, if I want to add the “line 4” to the linuxhintaddline file, I execute the command shown in the following screenshot:
I want to check the file to confirm the line was properly appended.
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:
If I want to add a “line 5” last line to the linuxhintaddline file, I run the command shown below.
Now, just to use a different command, I will confirm if the line was added by executing the command below.
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.
The syntax to append lines to files with root or sudo privileges is the following:
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.
Again, I use the less command to confirm the line was successfully added.
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:
Therefore, if I want to add the “line 6” line with privileges I type the command shown in the figure below.
To check if the line was properly added, I will run the less command again followed by the path.
And as you can see, the line was appended.
That’s how you can easily append lines to the end of files in Linux.
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.