Linux tee Command: Display and Save Pipeline Output

tee sends the standard input to the standard output while also recording the same content in one or more files. It is useful when you want to view intermediate results in a pipeline while saving them.

By default, it overwrites the specified file. To append to existing records, use -a. Unlike regular > redirection, it can continue to pass input to the following commands in the pipeline.

Basic syntax

Command | tee [options] file [file...]

Installed implementations and options may vary depending on the distribution. Check the description for your current system with man tee.

Examples

Simultaneously to the screen and file

Save to the file while viewing the output.

date | tee run-time.txt

Append to existing logs

-a appends to the end of the file.

date | tee -a run-time.txt

Save intermediate results and continue processing

After saving the sorting results, proceed to check only the beginning part.

sort names.txt | tee names.sorted | head

Results that appear on both the screen and a file

To view intermediate results of a pipeline on the screen while also saving them to a file, use tee.

printf 'status=ok\n' | tee result.txt

The following will be displayed on the screen, and the same line will be recorded in result.txt.

status=ok

The default behavior is to overwrite the target file. To keep the existing content and append to it, use tee -a result.txt. If writing to a file owned by the administrator, you may need command | sudo tee file instead of sudo command > file because redirection is executed with the current shell's permissions.

Main Options and Format

Options/Format Description
-a Add without overwriting the existing file.
-i Ignore interrupt signals.
-p Adjust the way GNU implementation handles pipe write errors.

Precautions when using

sudo command > /protected/file may fail because the current shell performing the redirection does not have permission. In this case, use command | sudo tee file, but the input will also be displayed on the screen, so be careful with secrets. Error output is not sent to tee by default.

Frequently Asked Questions

Does tee save standard error as well?

By default, it only saves what comes in through standard input. To include errors, explicitly specify shell redirection as appropriate.

Official Documentation

The exact behavior of the option and the differences between implementations can be found in the official documentation for tee.