Easy methods to Append Textual content to Finish of File in Linux

Whereas working with configuration information in Linux, typically it is advisable append textual content corresponding to configuration parameters to an current file. To append merely means so as to add textual content to the top or backside of a file.

On this brief article, you’ll be taught other ways to append textual content to the top of a file in Linux.

Append Textual content Utilizing >> Operator

The >> operator redirects output to a file, if the file doesn’t exist, it’s created but when it exists, the output will probably be appended on the finish of the file.

For instance, you should utilize the echo command to append the textual content to the top of the file as proven.

# echo “/mnt/pg_master/wal_archives 10.20.20.5(rw,sync,no_root_squash)” >> /and so on/exports

Alternatively, you should utilize the printf command (don’t forget to make use of n character so as to add the following line).

# printf “/mnt/pg_master/wal_archives 10.20.20.5(rw,sync,no_root_squash)n” >> /and so on/exports

You may also use the cat command to concatenate textual content from a number of information and append it to a different file.

Within the following instance, the extra file system shares to be appended within the /and so on/exports configuration file are added in a textual content file known as shares.txt.

# cat /and so on/exports
# cat shares.txt
# cat shares.txt >> /and so on/exports
# cat /and so on/exports

Append Files to /etc/exports

Append Recordsdata to /and so on/exports

Moreover, you may also use the next right here doc to append the configuration textual content to the top of the file as proven.

# cat /and so on/exports
# cat >>/and so on/exports /backups 10.20.20.zero/24(rw,sync)
> /mnt/nfs_all 10.20.20.5(rw,sync)
> EOF
# cat /and so on/exports

Append Text Using here Document

Append Textual content Utilizing right here Doc

Consideration: Don’t mistake the > redirection operator for >>; utilizing > with an current file will delete the contents of that file after which overwrites it. This may increasingly lead to information loss.

Append Textual content Utilizing tee Command

The tee command copies textual content from customary enter and pastes/writes it to straightforward output and information. You should utilize its -a flag to append textual content to the top of a file as proven.

# echo “/mnt/pg_master/wal_archives 10.20.20.5(rw,sync,no_root_squash)” | tee -a /and so on/exports
OR
# cat shares.txt | tee -a /and so on/exports

Append Text Using Tee Command

Append Textual content Utilizing Tee Command

You may also use a right here doc with the tee command.

# cat </backups 10.20.20.zero/24(rw,sync)
>/mnt/nfs_all 10.20.20.5(rw,sync)
EOF

Append Text Using Here and Tee Command

Append Textual content Utilizing Right here and Tee Command

You may additionally wish to learn these associated articles.

Easy methods to Run Instructions from Customary Enter Utilizing Tee and Xargs in Linux
Be taught The Fundamentals of How Linux I/O (Enter/Output) Redirection Works
Easy methods to Save Command Output to a File in Linux
Easy methods to Depend Phrase Occurrences in a Textual content File

That’s it! You’ve realized tips on how to append textual content to the top of a file in Linux. When you have questions or ideas to share, attain us through the suggestions type beneath.

LEAVE A REPLY

Please enter your comment!
Please enter your name here