Append Lines to a File in Linux

1. Introduction

In this tutorial, we’re going to explore several ways to append one or
more lines to a file in Linux using Bash commands.

First, we’ll examine the most common commands like echo, printf,
and cat.
Second, we’ll take a look at the tee command, a
lesser-known but useful Bash utility.

2. The echo Command

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

3. The printf Command

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

4. The cat Command

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 their contents to the standard
output, in the specified order.

Let’s suppose we already have some lines in file1.txt and we want to
append them to result.txt

cat file1.txt >> result.txt
cat file1.txt file2.txt file3.txt >> result.txt

Next, we’re going to remove 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 then type something into the terminal including new lines and
then press CTRL + D to exit:

[email protected]:~/Desktop/baeldung/append-lines-to-a-file$ cat >> file.txt
line1 using cat command
line2 using cat command
<press CTRL+D to exit>

This will add two lines to the end of file.txt.

5. The tee Command

Another interesting and useful Bash command is the
tee command. It reads data from
standard input and writes it 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:

[email protected]:~/Desktop/baeldung/append-lines-to-a-file$ tee -a file.txt
line1 using tee command

Once we hit Enter, we’ll actually see our same line repeated back to
us:

[email protected]:~/Desktop/baeldung/append-lines-to-a-file$ 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 input how many lines we want and hit the Enter key
after each line. We’ll notice that each line will be duplicated in the
terminal and also appended to our file.txt:

[email protected]:~/Desktop/baeldung/append-lines-to-a-file$ 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:

[email protected]:~/Desktop/baeldung/append-lines-to-a-file$ tee >> file.txt
line3 using tee command
<press CTRL+D to exit>

Finally, let’s take a look at the file.txt‘s contents:

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
line1 using tee command
line2 using tee command
line3 using tee command

6. Conclusion

In this tutorial, we’ve described a few ways that help us append one or
more lines to a file in Linux.

First, we’ve studied the echo, printf and cat Bash commands and
learned how we can combine them with the redirection operator in order
to append some text to a file. We’ve also learned how to append the
content of one or more files to another file.

Second, we’ve examined the lesser-known tee command that already has a
built-in mechanism of appending some text to a file and doesn’t
necessarily need the redirection operator.

Leave a Reply

Your email address will not be published.