1. Overview
In this tutorial, we’re going to explore several Linux commands to append one or more lines to a file.
Initially, we’ll look at common Linux basic tools like echo, printf, and cat. Then, we’ll explore a few lesser-known Bash utilities, such as tee and sed.
All commands that we’ll be using in this guide have been tested on Debian 12 (Bookworm) with GNU Bash 5.2.15, though they should also work on most other Linux distributions.
2. Using Linux Basic Tools
2.1. echo
The echo command is one of the most commonly and widely used built-in commands for Linux Bash. Usually, we can use it to display a string to standard output, which is the terminal by default:
$ echo "This line will be displayed to the terminal"
Now, we’re going to change the default standard output and divert the input string to a file. This capability is provided by the redirection operator (>). If the file specified below already contains some data, the data will be lost:
$ echo "This line will be written into the file" > file.txt
In order to append a line to our file.txt and not overwrite its contents, we need to use another redirection operator (>>):
$ echo "This line will be appended to the file" >> file.txt
Note that the ‘*>’ and ‘>>*’ operators are not dependent on the echo command and they can redirect the output of any command:
$ ls -al >> result.txt
$ find . -type f >> result.txt
Moreover, we can enable the interpretation of backslash escapes using the -e option. So, some special characters like the new line character ‘\n’ will be recognized and we can append multiple lines to a file:
$ echo -e "line3\n line4\n line5\n" >> file.txt
This will append three new lines to the file.txt.
2.2. printf
The printf command is similar to the C function with the same name. It prints any arguments to standard output in the format:
$ printf FORMAT [ARGUMENTS]
Let’s build an example and append a new line to our file using the redirection operator:
$ printf "line%s!" "6" >> file.txt
Unlike the echo command, we can see that the printf‘s syntax is simpler when we need to append multiple lines. Here, we don’t have to specify special options in order to use the newline character:
$ printf "line7\nline8!" >> file.txt
This will add two new lines to the end of file.txt.
2.3. cat
The cat command concatenates files or standard input to standard output.
It uses a syntax similar to the echo command:
$ cat [OPTION] [FILE(s)]
The difference is that, instead of a string as a parameter, cat accepts one or more files and copies its contents to the standard output, in the specified order.
Let’s suppose we already have some lines in one or more files and we want to append them to result.txt:
$ cat file1.txt >> result.txt
$ cat file1.txt file2.txt file3.txt >> result.txt
The first command appends the content of file1.txt to result.txt, while the second one appends the content of file1.txt, file2.txt, and file3.txt to result.txt.
Next, let’s learn another method by removing the input file from the command:
$ cat >> file.txt
In this case, the cat command will read from the terminal and appends the data to the file.txt.
So, let’s type something into the terminal including new lines and then press CTRL + D to exit:
$ cat >> file.txt
line1 using cat command
line2 using cat command
<press CTRL+D to exit>
This will add two new lines to the end of file.txt.
Here’s another method to let the cat command read the input until it detects certain text:
$ cat << EOF >> file.txt
line3 using cat command with EOF
line4 using cat command with EOF
EOF
In this example, << EOF* means that we want *cat* to read from the standard input until it encounters a line only containing the text *EOF* (end of file). Likewise, the output of *cat* is written to *file.txt* in the same way as in previous examples, which is by using the *>> redirection operator.
3. Using tee
Another interesting and useful Bash command is the tee command. It reads data from standard input and writes it both to the standard output and to files:
$ tee [OPTION] [FILE(s)]
$ tee file1.txt
$ tee file1.txt file2.txt
In order to append the input to the file and not overwrite its contents, we need to apply the -a option:
$ tee -a file.txt
line1 using tee command
Once we hit Enter, we’ll actually see our same line repeated back to us:
$ tee -a file.txt
line1 using tee command
line1 using tee command
This is because, by default, the terminal acts as both standard input and standard output.
We can continue to enter as many lines as we want and hit the Enter key after each line:
$ tee -a file.txt
line1 using tee command
line1 using tee command
line2 using tee command
line2 using tee command
<press CTRL+D to exit>
Now, let’s suppose we don’t want to append the input to the terminal, but only to a file. This is also possible with the tee command. Thus, we can remove the file parameter and redirect the standard input to our file.txt by using the redirection operator:
$ tee >> file.txt
line3 using tee command
<press CTRL+D to exit>
In addition, we can use tee in a way similar to how we used cat previously, which is by letting the tee command read the input until it detects certain text:
$ tee -a file.txt << EOF
line4 using tee command with EOF
line5 using tee command with EOF
EOF
Finally, let’s take a look at the file.txt‘s contents:
$ cat file.txt
This line will be written into the file
This line will be appended to the file
line3
line4
line5
line6
line7
line8
line1 using cat command
line2 using cat command
line3 using cat command with EOF
line4 using cat command with EOF
line1 using tee command
line2 using tee command
line3 using tee command
line4 using tee command with EOF
line5 using tee command with EOF
As we can see from the output above, we have successfully appended multiple lines to file.txt using a variety of Bash utilities.
4. Using sed
The sed command (short for “stream editor”) is a text processing tool to do basic text transformations on text files or streams:
$ sed [OPTION] [SCRIPT] [FILE...]
It is commonly used for performing find-and-replace operations, substitutions, deletions, appends, and many more. As for this article, let’s use it to append lines to a file.
First, let’s create a sample file using echo:
$ echo "This line will be written into the file" > file.txt
Next, let’s add a line to the file:
$ sed -i '$ a\This is a new line' file.txt
This command appends a line to file.txt in-place, using -i option to modify the file directly instead of writing the changes to standard output. It goes to the last line of the file ($) and uses the a\* option to indicate an append command. Then, the line ‘This is a new line*‘ will be appended to the file.
Now, let’s see what would happen if we didn’t add the -i option:
$ sed '$ a\This is line 3' file.txt
This line will be written into the file
This is a new line
This is line 3
$ cat file.txt
This line will be written into the file
This is a new line
The changes weren’t written to the file, but instead, were only displayed on the standard output.
Next, let’s add more than one line to the file:
$ sed -i -e '$a\This is line 3' -e '$a\This is line 4' file.txt
We use the -e option to indicate that we’ll be using multiple expressions or commands for sed.
Finally, let’s see the content of file.txt:
$ cat file.txt
This line will be written into the file
This is a new line
This is line 3
This is line 4
At this point, we have successfully created a new file file.txt using echo, then appended three new lines to the file using sed.
5. Conclusion
In this tutorial, we’ve explored several ways that could help us append one or more lines to a file.
In the beginning, we studied the echo, printf and cat Bash commands and learned how we can combine them with the redirection operator in order to append some texts to a file. We also learned how to append the content of one or more files to another file.
Lastly, we explored the lesser-known tee and sed commands that already have a built-in mechanism of appending some texts to a file, and don’t necessarily need the redirection operator.