1. Introduction

A typical task for a Linux user is to add text to a file. Moreover, this is significant especially when it comes to configuration files and logs.

In this tutorial, we’ll discuss some common Linux commands for appending text to a file with and without I/O redirection.

2. Common Redirect Operation

Perhaps the most popular method to append text to a file involves the > and >> operators.

In short, we’ll examine the difference between them using file.txt as an example:

$ cat file.txt 
This is the first line.

The >> operator adds its input as a new line to the end of a file. Let’s do this for file.txt using the echo command, which is used to print a string in the terminal:

$ echo "Hello World" >> file.txt 
$ cat file.txt
This is the first line. 
Hello World

On the other hand, using > truncates the file before adding its input:

$ echo "Hello World 2.0" > file.txt
$ cat file.txt
Hello World 2.0

In summary, when appending text to a file in Linux, the > operator overwrites the file’s contents, while >> appends to the end of the file.

3. Using tee Command

In this section, we’ll learn about using the tee command for appending text to a file. Basically, tee can duplicate the standard input to the standard output and a file.

Let’s use the tee command to append to file.txt from the previous section by adding a new line:

$ echo "This line is added by using tee." | tee -a file.txt 
Hello World 2.0 
This line is added by using tee.

In essence, the -a or –append option of tee appends to a file without the need for redirection operators.

4. Using awk Command

In addition, we can use the awk command to append text to a file. The awk command is a text processing tool that can be employed to manipulate text files according to our requirements:

$ echo "This line is added using the awk command." | awk '{print $0 >> "file1.txt"}' 
This line is added using the awk command.

In this example, awk uses the string from standard input. Moreover, the internal print statement appends that input to file1.txt. Here, the special variable $0 refers to the data on standard input.

5. Using perl Command

Alternatively, we can use perl as a powerful scripting language for a variety of text-processing tasks:

$ echo "This line is added using Perl." | perl -ne 'open(F, ">>file2.txt"); print F $_; close(F);' 
This line is added using Perl.

First, perl uses open() and >> to have file2.txt ready in append mode as F. Next, the interpreter gets the string on standard input from $_ and appends it to F. Finally, after writing to the file, the close() function closes it.

6. Using sed Command

The sed command allows us to append text to a file in several ways. However, the choice of option depends on our specific needs and the location where we want to append the text in the file:

$ sed -i '$ a\This line is appended using sed.' file10.txt 
This line is appended using sed.

Let’s elaborate on the options used in this example. Firstly, the -i option denotes an in-place file transformation. In our case, this means file10.txt is directly modified. Secondly, the $ symbol specifies the last line. Finally, the a prefix appends the text that follows to file10.txt, where \ acts as a delimiter.

7. Conclusion

In this article, we looked at different methods to add data to the end of a file.

In conclusion, there are multiple ways to append text to a file in Linux, including using tee, awk, perl, and sed. Each tool offers specific options and capabilities. Therefore, our final choice depends on our needs and where we want to add text to the file.

Comments are closed on this article!