Home Terminal Tuts How to append text to end of a file in Linux

How to append text to end of a file in Linux

by Hend Adel
Published: Updated:
Append text in Linux

Sometimes while working with text files, you just need to add new text at the end of the file without deleting its content. This operation is called appending in Linux.

Moreover, the append operation can be used with not just text; it can be used with commands where you can add the output of command at the end of a file.

Appending text to a file in Linux

In this tutorial, we are going to show you how to use the append operation in Linux systems using the terminal. We are going to cover the following four methods:

  • Redirect text to a file using the > operator
  • Append text to an existing file using >> operator
  • Append command output to an existing file
  • Append using a tee command

Before starting this tutorial, let’s first create a new empty file using the below command:

touch append_example
Create New Empty File

Create a New Empty File

Check if the file was created successfully. Also, note that the file size is Zero, which means it is an empty file.

ls -l
Example File Created Successfully

Example File Created Successfully

Method 1: Redirect text to a file using the > operator

Typically, the > operator can be used to add text to an already existing file. However, if the file is not found, it creates a new file. Moreover, each time the > operator is used, it overwrites the file content.

To overwrite a file content, use the > operator as follows:

echo 'hello world'  > append_example
Redirect The Output To A File

Redirect The Output To A File

To check and display the file content using the cat command as following:

cat append_example
Content Of the Example File 1

Content Of the Example File 1

Method 2: Append text to an existing file using >> operator

In this method, the >> operator can be used to append text to the end of a file without overwriting its content. Similarly, if the file was not found, the command creates a new file.

Use the >> operator to append text as following:

echo 'this is the second line' >> append_example
Append The Output To A File and Do not Overwrite it

Append The Output To A File and Do not Overwrite it

To display the file content:

cat append_example
Content Of the Example File 2

Content Of the Example File 2

As you can see, using the >> operator, the text was added to the end of the file and did not overwrite the file content.

Method 3: Append command output to an existing file

Here we are going to append a command output to the end of a file.

Append the current working directory variable value to a file as follows:

echo $PWD >> append_example
Append Command Output To A File and Do not Overwrite it

Append Command Output To A File and Do not Overwrite it

Display the file content as following:

cat append_example
Content Of the Example File 3

Content Of the Example File 3

Also, you can use any other command to append its content to a file.

date >> append_example
Append Date Command Output To A File

Append Date Command Output To A File

Display the file content.

cat append_example
Content Of the Example File 4

Content Of the Example File 4

Method 4: Append using a tee command

Additionally, you can use the tee command to append text. Before using the tee, command let’s first create a second example file that we use in the tee command.

Create a second example file and add some text to it as follows:

echo '11111111111' > append_example2
Create Another Example File

Create Another Example File

Display the content of the second example file:

cat append_example2
Content Of the Second Example File

Content Of the Second Example File

Now let’s use the tee command to append the content of the one file to another file as following.

cat append_example2 | tee -a append_example
Append Using tee Command

Append Using tee Command

Then you can display the content of the file as follows:

cat append_example
Content Of the Example File 5

Content Of the Example File 5

Conclusion

That’s all about various ways of appending text to a file in Linux. What other exciting ways do you prefer? Let us know in the comments below, and please share the article with your friends if you liked the article.

You may also like

Leave a Comment

fl_logo_v3_footer

ENHANCE YOUR LINUX EXPERIENCE.



FOSS Linux is a leading resource for Linux enthusiasts and professionals alike. With a focus on providing the best Linux tutorials, open-source apps, news, and reviews written by team of expert authors. FOSS Linux is the go-to source for all things Linux.

Whether you’re a beginner or an experienced user, FOSS Linux has something for everyone.

Follow Us

Subscribe

©2016-2023 FOSS LINUX

A PART OF VIBRANT LEAF MEDIA COMPANY.

ALL RIGHTS RESERVED.

“Linux” is the registered trademark by Linus Torvalds in the U.S. and other countries.