How to append text to the end of a file in Linux

If you want to know how to append text to the end of a file in Linux, then you are in the right place. Folks, here we will discuss what are the various ways to append text to a file in Linux.

Common Scenarios for appending the text to a file

  • appending the logs to a log file, generally done by servers.
  • saving the output of a command to a file. For example, saving the output of a script to a file for debugging purposes later on.
  • copying the content of a file to another file. For example, creating a backup file before making any change to a file.
  • concatenating a group of files content to another file. For example, merging a bunch of CSV files to create a new CSV file.

Linux Commands to append text to a File

We use redirection operator (>>) to append data to an existing text file. However, we need to provide the data to be appended to the file. Some of the common commands that are used along with >> operator are cat, echo, print, etc.

Let’s look at some examples to append text to a file in Linux.

1. Append text to the end of a file using redirection operators

Adding the text is possible with the “>” and “>>” operators. These are the output redirection operators. Redirection is sending the output to any file.

There are two types of redirection operator i.e. Input redirection and Output Redirection. <” is called as Input redirection while “>” is called Output redirection.

For your understanding, here’s what input redirection looks like.

tr 'a-z' 'A-Z' < linux.txt
Input Redirection
Input Redirection

Here, the input from the file i.e. linux.txt is sent to the tr command through input redirection operator. It will print the characters in uppercase as shown above in the image.

Talking about the Output redirection operator, it is used to send the output of the command to a file. It can be done using any command either cat or echo. Let’s understand it by the following example,

cat > linux.txt 
Hello World
This is blog based on how to append the text in Linux
cat linux.txt
Text Step1
Text using > operator

Using “>” operator would send the output of the cat command to the linux.txt file. Further, I have used cat command without “>” operator because here we not going to append text to the end of a file but are just displaying the content of the file.

We will consider the linux.txt file for further examples.

Similarly, “>>” redirection operator is used to append text to the end of a file whereas “>” operator will remove the content in the existing file. I hope the difference is cleared between both of them. Don’t worry, we will discuss this in more detail.

Let’s dive into the ways on how can we append the text in the file using various commands.

2. Using cat command with redirection operator (>>)

The “>>” operator is used to append text to the end of a file that already has content. We’ll use the cat command and append that to our linux.txt file.

Consider we want to append text to the end of a file i.e. linux.txt as shown above. Let’s have a look at the command below:

cat >> [file_name]
Append Text Using Cat Command Step 2
Append Text Using Cat Command

As shown above, the input text is sent to the file i.e. linux.txt. WE use the cat command once again with the append operator to print the text as shown in the example above.

You can see that the text has been appended at the bottom of the file. Remember while adding the text using “>>” operator through cat command, you’ll be working within the editor mode. To save the text and finish appending, you can press Ctrl+D.

3. Linux echo command with >>

The echo command is used to print the text, print the value of the variable and so on. If you want to have detail knowledge, you can check the tutorial on the echo command in Linux. While using the echo command, you need to add the text within quotations. The output of the echo command will be redirected through the”>>” operator to the file. Let’s have a look at the below command.

echo 'text' >> [file_name]
Append Text Using Echo Command Step 3
Append Text Using Echo Command

You can see that the text “I hope you understood the concept.” is appended at the bottom of the file. You can print the content of the file using cat command. Here also, the text is sent as output to the file through “>>” operator.

4. The tee command with -a option

The tee command is used to read the standard input and writes it to the standard output as well as files too. Using tee command with -a option will not overwrite the content but will append it to the file. Let’s understand through an example.

cat [file_name1] | tee -a [file_name2]
Append The Text Using Tee Command Step 6
Append The Text Using Tee Command

The text in the linux.txt file is redirected to tee command through “|” operator and is appended to the file linuxfordevices.txt. You can see that all the data is transferred to the new file.

5. Append using”printf” in Linux

You might be thinking why we are using printf in Linux as it is used in C language to print the text. We know that printf is used to display the standard output. Folks, we can also use printf command to append the text using “>>” operator in Linux. Just add the text within quotations and send it to the file as output. Let’s have a look at the command below:

printf "Text" >> [file_name]
Append The Text Using Printf Step 5
Append The Text Using Printf

6. Append the text using awk command

The awk command is used to perform the specific action on the text as directed by the program statement. Using the BEGIN keyword with awk command specifies that awk will execute the action as denoted in begin once before any input lines are read.

Therefore, we will provide the command enclosed within the BEGIN as shown below in the example. The command to append the text is provided in the BEGIN section, hence awk command will execute it once it reads the input lines. Let’s have a look at the command below:

awk 'BEGIN{ printf "TEXT" >> "File_name" }' 
Append Text Using Awk Command Stpe 7
Append Text Using Awk Command

You can see that the text “Feel free to reach us” has been appended to the bottom of the linux.txt file as shown above in the image. Use the cat command to print the text of the file.

7. Save the list of files to another file using >> operator

We can also save the list of the files in any directory using ls command with “>>” operator. We have to just use ls command followed by the “>>” operator and the file name where we need to save the output. Let’s have a look at the command below:

ls >> [filename]
List Atll The Files Step 9 1
List All The Files

Conclusion

In this tutorial, we learned how to use the “>>” operator to append text to the end of a file. We also learned what is the difference between input redirection and output redirection, how to save the list of files and date to any file and so on. I hope that all your concerns are cleared. If face any issues, do let us know in the comment section.