How to Easily Append Text to End of File in Linux

User Avatar
👤 admin
đź”´ Admin
✍️ The most important thing in the world is to not be alone.
⏳ Last active: 15 Apr 2025 at 16:01
đź“… Created: 27 Jun 2021 at 11:18
đź‘€ Viewed: 20 times
✉️ Send Email

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 ```

>>


For example, you can use the <a href="https://www.tecmint.com/echo-command-in-linux/" target="_blank" rel="noreferrer noopener">echo command</a> to append the text to the end of the file as shown.

$ echo "This is some example text to add to file exports" >> /etc/exports


Alternatively, you can use the <strong>printf</strong> command (do not forget to use ```

\n

``` character to add the next line).

$ printf "This is some example text to add to file exports\n" >> /etc/exports


You can also use the&nbsp;<a href="https://www.tecmint.com/13-basic-cat-command-examples-in-linux/" target="_blank" rel="noreferrer noopener">cat command</a>&nbsp;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 <strong>/etc/exports</strong> configuration file are added in a text file called <strong>shares.txt</strong>.

$ cat /etc/exports

$ cat shares.txt

$ cat shares.txt >> /etc/exports

$ cat /etc/exports


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

$ cat /etc/exports

$ cat >>/etc/exports<s<EOF

This is some example text to add to file exports

This is some example text to add to file exports 2

EOF

$ cat /etc/exports


<strong>Attention</strong>: Do not mistake the&nbsp;```

&gt;

```&nbsp;redirection operator for&nbsp;```

&gt;&gt;

```; using&nbsp;```

&gt;

```&nbsp;with an existing file will delete the contents of that file and then overwrites it. This may result in data loss.

<h3>Append Text Using tee Command</h3>

The <strong>tee command</strong> 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 "This is some example text to add to file exports" | tee -a /etc/exports

OR

$ cat shares.txt | tee -a /etc/exports


You can also use a <strong>here document</strong> with the <strong>tee command</strong>.

$ cat <<EOF | tee -a /etc/exports

This is some example text to add to file exports

This is some example text to add to file exports 2

EOF



Conclusion

That’s it! You have learned how to append text to the end of a file in Linux. We hope you enjoyed this <a href="https://lateweb.info/category/linux/">article</a>. if that is so please rate this page with the stars bellow and subscribe to our <a href="https://www.youtube.com/channel/UCh7Q9uaAt5-Z2lCZXX3OsvQ">YouTube channel</a> or follow us on <a href="https://twitter.com/liuskatali">twiter</a>.

- Another article that you can be interested in is <a href="https://lateweb.info/10-very-stupid-linux-commands-some-of-them-deadly/">10 Very Stupid Linux Commands [ Some Of Them Deadly ]</a>

- or <a href="https://lateweb.info/worst-linux-distros-for-beginners-and-what-to-choose/">Worst Linux Distros for Beginners [ And What To Choose ]</a><br>———————————————————————————————————————
If you want to comment: Login or Register