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.
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 <a href="https://www.tecmint.com/13-basic-cat-command-examples-in-linux/" target="_blank" rel="noreferrer noopener">cat command</a> 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 ```
>
``` 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.
<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>———————————————————————————————————————