How to append text to a system file via terminal with sudo command

Print Friendly, PDF & Email
How to use tee command to append a file
How to use tee command to append a file

Working with Unix-based systems, I have gradually become sluggish to open an editor and tweak something in a file. Especially, system files must only be allowed by a superuser to open which causes a bit complicated and bored. If you are not familiar with Unix-based systems, you probably have some trouble with changing such files.

As the post title suggests, here is the tip.

echo ' router' |  sudo tee -a /etc/hosts


Below is the output of running man tee

tee -- pipe fitting

tee [-ai] [file ...]

The tee utility copies standard input to standard output, making a copy in zero or more files. The output is

The following options are available:

-a Append the output to the files rather than overwriting them.

-i Ignore the SIGINT signal.

The following operands are available:

file A pathname of an output file.

The tee utility takes the default action for all signals, except in the event of the -i option.

The tee utility exits 0 on success, and >0 if an error occurs.

The tee function is expected to be POSIX IEEE Std 1003.2 (``POSIX.2'') compatible.

Running this command is recommended to take more extra care because it will destroy everything saved and persisted on the target files if you forget to indicate the option -a meaning append. I used to be suffered such an incident without having any recoverable way.

Hopefully, this tip helps you improve your productivity on a daily basis.

All constructive comments are always welcomed in the comment section below. If you are interesting our free sharing posts, please consider a small but financial contribution for encouraging our work by clicking the link Buy Me A Coffee in the box below.

Leave a Reply

Your email address will not be published. Required fields are marked *



This site uses Akismet to reduce spam. Learn how your comment data is processed.