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.

What is the tee command?

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.

More in This Category
Linux who Command: List Logged-In Users

Linux who Command: List Logged-In Users

Learn how to list Logged-In Users with the Linux who command, including practical examples, key options, and important precautions.

Linux zip Command: Create ZIP Archives

Linux zip Command: Create ZIP Archives

Learn how to create ZIP Archives with the Linux zip command, including practical examples, key options, and important precautions.

Linux fg Command: Bring a Shell Job to the Foreground

Linux fg Command: Bring a Shell Job to the Foreground

Learn how to bring a Shell Job to the Foreground with the Linux fg command, including practical examples, key options, and important precautions.

Linux nslookup Command: Query DNS Names and Records

Linux nslookup Command: Query DNS Names and Records

Learn how to query DNS Names and Records with the Linux nslookup command, including practical examples, key options, and important precautions.

Linux tmux Command: Detach and Reattach Terminal Sessions

Linux tmux Command: Detach and Reattach Terminal Sessions

Learn how to detach and Reattach Terminal Sessions with the Linux tmux command, including practical examples, key options, and important precautions.

Linux apt-get Command: Run APT Package Operations in Scripts

Linux apt-get Command: Run APT Package Operations in Scripts

Learn how to run APT Package Operations in Scripts with the Linux apt-get command, including practical examples, key options, and important precautions.

Linux cut Command: Extract Characters and Fields

Linux cut Command: Extract Characters and Fields

Learn how to extract Characters and Fields with the Linux cut command, including practical examples, key options, and important precautions.

Linux printf Command: Print Values with a Defined Format

Linux printf Command: Print Values with a Defined Format

Learn how to print Values with a Defined Format with the Linux printf command, including practical examples, key options, and important precautions.

Linux ln Command: Create Hard Links and Symbolic Links

Linux ln Command: Create Hard Links and Symbolic Links

Learn how to create hard links and symbolic links with Linux ln, choose relative or absolute targets, replace links safely, and inspect link behavior.

Linux time Command: Measure Command Execution Time

Linux time Command: Measure Command Execution Time

Learn how to use the Linux time command to measure elapsed, user CPU, and system CPU time for a command, with essential options, practical examples, output interpretation, and common troubleshooting tips.