Tips on how to Append Textual content to Finish of File in Linux

Whereas working with configuration recordsdata in Linux, typically it’s worthwhile to append textual content akin 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 quick article, you’ll study alternative 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 need to use 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 forth/exports

Alternatively, you need to use 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 forth/exports

You can even use the cat command to concatenate textual content from a number of recordsdata and append it to a different file.

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

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

Append Files to /etc/exportsAppend Files to /etc/exports

Append Information to /and so forth/exports

Moreover, it’s also possible to use the next right here doc to append the configuration textual content to the top of the file as proven.

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

Append Text Using here DocumentAppend 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 will lead to information loss.

Append Textual content Utilizing tee Command

The tee command copies textual content from normal enter and pastes/writes it to straightforward output and recordsdata. 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 forth/exports
OR
# cat shares.txt | tee -a /and so forth/exports

Append Text Using Tee CommandAppend Text Using Tee Command

Append Textual content Utilizing Tee Command

You can even 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 CommandAppend Text Using Here and Tee Command

Append Textual content Utilizing Right here and Tee Command

You may additionally prefer to learn these associated articles.

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

That’s it! You have got realized easy methods to append textual content to the top of a file in Linux. If in case you have questions or ideas to share, attain us through the suggestions type beneath.

Source link

LEAVE A REPLY

Please enter your comment!
Please enter your name here