tee command in Linux is a versatile utility often employed to read from
standard input and write to standard output and files simultaneously. It is
named after the T-splitter used in plumbing systems that equally divides the
flow. In computing,
tee works similarly by taking the data from standard
input (stdin) and duplicating it into two different directions: to standard
output (stdout) and one or more files.
The general syntax for the
tee command is:
tee [OPTION]... [FILE]...
[OPTION] is optional, and it specifies how the
tee command should
[FILE] denotes the files where the content should be saved.
|Append to the given files, do not overwrite them.
|Ignore interrupt signals.
|Display the help message and exit.
|Display the version information and exit.
Lets create a file called
This is line 1
This is line 2
This is line 3
Write to a File and Standard Output
The most straightforward use case for
tee is writing data to a file and the
For example, the following command will copy the content of
output.txt while also displaying the content on the terminal:
cat input.txt | tee output.txt
Append to a File and Standard Output
To append the data to an existing file rather than overwriting it, use the
echo "This is a new line" | tee -a output.txt
This will append the line "This is a new line" to
output.txt and display it on
If you want to make
tee ignore interrupt signals (like
Ctrl+C), use the
cat input.txt | tee -i output.txt
Writing to Multiple Files
tee allows you to write to multiple files at the same time. Simply specify the
filenames separated by spaces:
echo "Multifile Example" | tee file1.txt file2.txt file3.txt
Combining Multiple Options
Options can be combined for more specific functionality. To write to multiple files and append to them:
echo "Appending to multiple files" | tee -a file1.txt file2.txt
tee with Other Commands
tee is commonly combined with other commands to enable various workflows. One
such example is using
sudo to write to files owned by the root:
echo "new content" | sudo tee -a /etc/some_config_file > /dev/null
> /dev/null suppresses the output to the terminal but still allows
to append "new content" to a system file that needs root permissions.
tee command is a simple but powerful tool for duplicating data streams in
a shell pipeline. Its ability to write or append data to one or more files while
also sending it to standard output makes it incredibly useful in scripting and
day-to-day tasks. Understanding the options and combinations can help you use
this command more effectively.
What Can You Do Next 🙏😊
If you liked the article, consider subscribing to Cloudaffle, my YouTube Channel, where I keep posting in-depth tutorials and all edutainment stuff for software developers.