Skip to main content

Using the `tee` Command - Read From `stdin`, Ouput to `stdout` and Files

The 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.

Syntax​

The general syntax for the tee command is:

tee [OPTION]... [FILE]...

Here [OPTION] is optional, and it specifies how the tee command should behave, while [FILE] denotes the files where the content should be saved.

Options Table​

OptionShorthandDescription
--append-aAppend to the given files, do not overwrite them.
--ignore-interrupts-iIgnore interrupt signals.
--helpDisplay the help message and exit.
--versionDisplay the version information and exit.

Basic Usage​

Lets create a file called input.text

Creating input.text file
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 terminal simultaneously.

For example, the following command will copy the content of input.txt to 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 -a or --append option.

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 the terminal.

Ignore Interrupts​

If you want to make tee ignore interrupt signals (like Ctrl+C), use the -i or --ignore-interrupts option:

cat input.txt | tee -i output.txt

Advanced Usage​

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

Using tee with Other Commands​

tee is commonly combined with other commands to enable various workflows. One such example is using tee with sudo to write to files owned by the root:

echo "new content" | sudo tee -a /etc/some_config_file > /dev/null

Here, > /dev/null suppresses the output to the terminal but still allows tee to append "new content" to a system file that needs root permissions.

Conclusion​

The 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.

YouTube @cloudaffle