How to append text to end of a file in Linux

To append text to a file means adding text to the end of a file without overwriting the file content. In this tutorial, we shall show you how to use the append operation in Linux systems using the terminal. We are going to cover four methods.

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.

Hend Adel
Hi! I'm Hend Adel, a freelancer technical geek with successful experience in Database, Linux and many other IT fields. I help to build solutions to suit business needs and creating streamlined processes. I love Linux and I'm here to share my skills via FOSS Linux! Thanks for reading my article.

LEAVE A REPLY

Please enter your comment!
Please enter your name here

STAY CONNECTED

23,437FansLike
374FollowersFollow
16SubscribersSubscribe

LATEST ARTICLES

How to change Hostname on Ubuntu

Generally, a hostname is just an assigned name to a computer, laptop, or any other connected device to a network. You can say a hostname is a computer's nickname, and it is used to identify the machine over the network uniquely.

Manjaro vs. Ubuntu – which is better for you?

If you are a person associated with Computer technology and spend most of the time in the open-source arena, you must have heard or worked with some of the popular Linux distributions we have in the market. Some of the names that you will never miss are; Ubuntu, Arch Linux, Debian, and Mint.

9 Useful Tips Working with Operating System using Python

There are several situations when we want to work with the operating system using python. We may want to see the user details or want to do some tasks with the files and directories. If you are a system administrator, you will find it useful to work with Operating System as one can easily automate some repeating tasks of the Operating System using python.

System76 Lemur Pro Review

Buyers who wish to go for a machine that is based on Linux often show interest in Chromebooks due to the form factor and extended battery life capabilities. Although ChromeOS power these machines, users can still miss out on a more genuine Linux experience. For those who happen to agree, the new Lemur Pro by System76 might get some heads turning.

Top 10 New Features in Linux Kernel 5.8

Linus Torvalds recently announced the release of Linux Kernel 5.8, and he seems delighted with it. He has pointed it out as the most significant release of all time. To developers, this new kernel comes with an addition of 800,000 new code lines and more than 14,000 changed files. To the average user, you might not see many eye-candy changes, as seen in the earlier releases.

How to install Flutter on Linux

Do you want to install Flutter and start creating awesome Android and iOS applications? Then you have come to the right place. Here is a comprehensive tutorial on how to install Flutter on Linux. If you are new to Flutter, don't worry, we got that covered too as we shall go in-depth understanding of Flutter and what it brings to the table.

MUST READ

Buyers who wish to go for a machine that is based on Linux often show interest in Chromebooks due to the form factor and extended battery life capabilities. Although ChromeOS power these machines, users can still miss out on a more genuine Linux experience. For those who happen to agree, the new Lemur Pro by System76 might get some heads turning.
Linux is growing faster than ever. As per the latest report, there is a drop in the Windows 10 market share for the first time, and Linux's market share has improved to 2.87% this month. Most of the features in the list were rolled out in the Pop OS 20.04. Let's a detailed look into the new features, how to upgrade, and a ride through video.

How to clone hard disk on Linux using Clonezilla

Disk cloning refers to the process of copying data from one disk to another, thus creating a one-to-one copy of the drive. Technically, this process is possible using the copy-and-paste method.

VIDEO: Linux Lite 4.8 Features and Desktop Tour

Linux Lite eases Windows 7 users transition to Linux much more comfortable by offering simple software like Team Viewer, VLC, Firefox, TimeShift backup utility, and a full Microsoft Office compatible office suite in LibreOffice.

6 cool and fun Linux commands you don’t want to miss

The real purpose of this article is to help Linux newbies get comfortable and confident with the Linux command-line.  While knowledge and comfort of the Linux GUI allow great power, it is the mastery of the command line, or CLI, affords the Linux user unlimited power and certifies them as a Linux power user well on their way to becoming an expert.

How to dual-boot Ubuntu and Fedora on your PC

Every Linux distribution has its capabilities and preferences as per the user requirements. That brings forth the need to run different distros on one laptop. For example, a CyberSecurity enthusiast might prefer to use Ubuntu for development and Kali Linux for penetration testing.