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 groups Command: Show a User's Group Memberships

Linux groups Command: Show a User's Group Memberships

Learn how to show a User's Group Memberships with the Linux groups command, including practical examples, key options, and important precautions.

Linux less Command: Browse and Search Long Text Files

Linux less Command: Browse and Search Long Text Files

Learn how to browse long files with Linux less, move forward and backward, search text, follow growing logs, preserve colors, and exit the pager.

Linux ssh-keygen Command: Generate SSH Keys and Inspect Fingerprints

Linux ssh-keygen Command: Generate SSH Keys and Inspect Fingerprints

Learn how to generate SSH Keys and Inspect Fingerprints with the Linux ssh-keygen command, including practical examples, key options, and important precautions.

Linux xz Command: Compress a File in xz Format

Linux xz Command: Compress a File in xz Format

Learn how to compress a File in xz Format with the Linux xz command, including practical examples, key options, and important precautions.

Linux parted Command: Inspect and Manage Partitions

Linux parted Command: Inspect and Manage Partitions

Learn how to inspect and Manage Partitions with the Linux parted command, including practical examples, key options, and important precautions.

Linux systemctl Command: Manage systemd Services

Linux systemctl Command: Manage systemd Services

Learn how to manage systemd Services with the Linux systemctl 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.

A History of Linux: From a 1991 Hobby Project to Global Infrastructure

A History of Linux: From a 1991 Hobby Project to Global Infrastructure

Trace the history of Linux from Unix and the GNU Project through the first Linux kernel, distributions, enterprise adoption, cloud computing, containers, and modern infrastructure.

Linux w Command: Show Logged-In Users and Their Processes

Linux w Command: Show Logged-In Users and Their Processes

Learn how to show Logged-In Users and Their Processes with the Linux w command, including practical examples, key options, and important precautions.

Linux paste Command: Join Corresponding Lines Side by Side

Linux paste Command: Join Corresponding Lines Side by Side

Learn how to join Corresponding Lines Side by Side with the Linux paste command, including practical examples, key options, and important precautions.