How to Append Text to End of File in Linux

While working with configuration files in Linux, sometimes you need to append text such as configuration parameters to an existing file. To append simply means to add text to the end or bottom of a file.

In this short article, you will learn different ways to append text to the end of a file in Linux.

Append Text Using >> Operator

The >> operator redirects output to a file, if the file doesn’t exist, it is created but if it exists, the output will be appended at the end of the file.

For example, you can use the echo command to append the text to the end of the file as shown.

# echo "/mnt/pg_master/wal_archives     10.20.20.5(rw,sync,no_root_squash)" >> /etc/exports

Alternatively, you can use the printf command (do not forget to use \n character to add the next line).

# printf "/mnt/pg_master/wal_archives     10.20.20.5(rw,sync,no_root_squash)\n" >> /etc/exports

You can also use the cat command to concatenate text from one or more files and append it to another file.

In the following example, the additional file system shares to be appended in the /etc/exports configuration file are added in a text file called shares.txt.

# cat /etc/exports
# cat shares.txt
# cat shares.txt >>  /etc/exports
# cat /etc/exports
Append Files to /etc/exports
Append Files to /etc/exports

Besides, you can also use the following here document to append the configuration text to the end of the file as shown.

# cat /etc/exports
# cat >>/etc/exports<s<EOF
> /backups 10.20.20.0/24(rw,sync)
> /mnt/nfs_all 10.20.20.5(rw,sync)
> EOF
# cat /etc/exports
Append Text Using here Document
Append Text Using here Document

Attention: Do not mistake the > redirection operator for >>; using > with an existing file will delete the contents of that file and then overwrites it. This may result in data loss.

Append Text Using tee Command

The tee command copies text from standard input and pastes/writes it to standard output and files. You can use its -a flag to append text to the end of a file as shown.

# echo "/mnt/pg_master/wal_archives     10.20.20.5(rw,sync,no_root_squash)" | tee -a /etc/exports
OR
# cat shares.txt | tee -a /etc/exports
Append Text Using Tee Command
Append Text Using Tee Command

You can also use a here document with the tee command.

# cat <<EOF | tee -a /etc/exports
>/backups 10.20.20.0/24(rw,sync)
>/mnt/nfs_all 10.20.20.5(rw,sync)
EOF
Append Text Using Here and Tee Command
Append Text Using Here and Tee Command

You might also like to read these related articles.

  1. How to Run Commands from Standard Input Using Tee and Xargs in Linux
  2. Learn The Basics of How Linux I/O (Input/Output) Redirection Works
  3. How to Save Command Output to a File in Linux
  4. How to Count Word Occurrences in a Text File

That’s it! You have learned how to append text to the end of a file in Linux. If you have questions or thoughts to share, reach us via the feedback form below.

If you read this far, tweet to the author to show them you care. Tweet a thanks
Aaron Kili
Aaron Kili is a Linux and F.O.S.S enthusiast, an upcoming Linux SysAdmin, web developer, and currently a content creator for TecMint who loves working with computers and strongly believes in sharing knowledge.

Each tutorial at TecMint is created by a team of experienced Linux system administrators so that it meets our high-quality standards.

Join the TecMint Weekly Newsletter (More Than 156,129 Linux Enthusiasts Have Subscribed)
Was this article helpful? Please add a comment or buy me a coffee to show your appreciation.

4 thoughts on “How to Append Text to End of File in Linux”

    • @Chris,

      I understand your concerns, but ads are the only option to keep this site running, pay to our authors, and feed my family…

      I hope you understand, also the ads are placed properly without affecting user navigation. If you still feel it’s hard to read, please contact me here [email protected].

      Reply
      • That’s a very rough and inappropriate response, Ravi, especially when is coming from a Mod, or the owner of the website.

        You could well have said:
        we’ll see if we can re-order the ads or place the ads in some other places, and not on the main body of the article, perhaps at the end of every reading. Learn to be more polite when answering to your readers.

        Reply
        • @Chris,

          Sorry bro, if my message hurts you. I will try to re-order the placement of ads in-article body as suggested by you..

          Reply

Got something to say? Join the discussion.

Thank you for taking the time to share your thoughts with us. We appreciate your decision to leave a comment and value your contribution to the discussion. It's important to note that we moderate all comments in accordance with our comment policy to ensure a respectful and constructive conversation.

Rest assured that your email address will remain private and will not be published or shared with anyone. We prioritize the privacy and security of our users.