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,648FansLike
360FollowersFollow
16SubscribersSubscribe

LATEST ARTICLES

How to install CMake on Ubuntu

CMake is a cross-platform free and open-source software tool designed to build, test, and package the software. CMake uses a simple platform and compiler-independent configuration files to control the software compilation process.

How to install Lightworks on Ubuntu

Even though Linux may not get a native installer of video editing software like Adobe Premiere or Final Cut Pro, that doesn't mean there are no industry standards tools available. Lightworks is non-linear editing (NLE) video mastering app for Windows, Linux, and macOS. Installing it on Ubuntu is simple due to deb package availability.

How to install DaVinci Resolve on Fedora

Davinci Resolve is a professional application used for color correction, video editing, visual effects, and motion graphics. It is one of the extensively used software by movie industries located in Hollywood.

The 10 Best Programming Languages for Hacking

One of the significant entities we have in Cyber Security is Ethical Hacking (ETH). It is the process of detecting and finding flaws or vulnerabilities in a system that a hacker would exploit.

5 Ways to Open a Terminal in Ubuntu

Even though Ubuntu supports many applications with amazing Graphical User Interfaces (GUI), there are always reasons why users prefer using the Terminal to perform different tasks.

How to install Wine on Fedora Workstation

Linux distributions are becoming more and more popular every day, and Fedora Workstation is not left behind. This popularity brings forth the need to run Windows applications on Linux distros like Fedora. Windows has quite some excellent Software that is not available for Linux.

MUST READ

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.
Elementary OS 5.1 Hera has received a point release with a handful of new features and bug fixes, and we will be reviewing the significant changes in this article. For those new to elementary OS, this Ubuntu-based Linux distribution uses their inhouse built Pantheon desktop environment and AppCenter.

5 Best Music Players for Linux

For sure, there will be Linux users out there who cannot live without music. If you're one of those people, you would know how important it is to have a good music player installed on your computer system. In this article, we present to you the 5 Best Music Players for Linux, which will hopefully be able to satisfy each of our readers' needs.

Ubuntu 19.10 (Eoan Ermine) Beta Installation and Overview

It may be early, but I've been looking forward to the release of Ubuntu 19.10 for some time now. As an impatient person, and promised readers in the FOSSLinux article announcing the release of Eoan Ermine that we would provide you a review of Ubuntu 19.10 Eoan Ermine - Beta.  So, here it goes.

6 Best Linux Distros for Laptops

Whether buying a Linux pre-installed laptop or selecting a Linux distro for your existing laptop, there are many things to consider. Let's take you through some of the best Linux distros that are optimized for Laptops in this 2019 edition of the article. Read on.

6 ways to find out your Linux file system type

Any Operating system in the market whether its Windows, Linux, Unix, macOS, and any other, must be able to access and manage files and data on storage devices.