How To Use Tee Command.
In this article, we’ll cover the basics of using the tee command.
The tee command reads from the standard input and writes to both standard output and one or more files at the same time.
tee Command Syntax
The syntax for the tee command is as follows:
-a (–append) – Do not overwrite the files instead append to the given files.
-i (–ignore-interrupts) – Ignore interrupt signals.
Use tee –help to view all available options.
FILE_NAMES – One or more files. Each of which the output data is written to.
How to Use the tee Command
The most basic usage of the tee command is to display the standard output (stdout) of a program and write it in a file.
You can view the content of the disk_usage.txt file using the cat command .
Write to Multiple File
The tee command can also write to multiple files. To do so, specify a list of files separated by space as arguments:
Append to File
By default, the tee command will overwrite the specified file.
To ignore interrupts use the -i (–ignore-interrupts) option. This is useful when stopping the command during execution with CTRL+C and want tee to exit gracefully.
Hide the Output
If you don’t want tee to write to the standard output, you can redirect it to /dev/null:
Using tee in Conjunction with sudo
Let’s say you want to write to a file that is owned by root as a sudo user. The following command will fail because the redirection of the output is not performed by sudo. The redirection is executed as the unprivileged user.
The output will look something like this:
Simply prepend sudo before the tee command as shown below:
tee will receive the output of the echo command , elevate to sudo permissions and write to the file.
Using tee in conjunction with sudo allows you to write to files owned by other users.
The tee command reads from standard input and writes it to standard output and one ore more files.