LinuxCommandLibrary

tee

TLDR

Write stdin to file and stdout

$ echo "text" | tee [file]
copy
Append to file instead of overwriting
$ echo "text" | tee -a [file]
copy
Write to multiple files
$ echo "text" | tee [file1] [file2]
copy
Write command output to file while viewing it
$ [command] | tee [output.log]
copy
Use with sudo to write to protected files
$ echo "text" | sudo tee [/etc/file]
copy
Write to file and pipe to another command
$ [command] | tee [file] | [another_command]
copy
Suppress stdout (write to file only)
$ [command] | tee [file] > /dev/null
copy

SYNOPSIS

tee [options] [file...]

DESCRIPTION

tee reads from standard input and writes to both standard output and one or more files simultaneously. Named after the T-shaped pipe fitting in plumbing, it "splits" the data stream.
Common uses include logging command output while still viewing it, saving intermediate pipeline results for debugging, and writing to files requiring elevated privileges via sudo.
Without -a, tee overwrites existing files. With multiple files, the same content is written to all of them.
Tee continues the pipeline—output goes to both files and stdout, allowing further processing with additional pipe stages.

PARAMETERS

-a, --append

Append to files instead of overwriting
-i, --ignore-interrupts
Ignore SIGINT (interrupt signal)
-p
Diagnose errors writing to non-pipes
--output-error[=mode]
Set behavior on write error (warn, warn-nopipe, exit, exit-nopipe)
--help
Display help and exit
--version
Display version and exit

CAVEATS

For writing to protected files with sudo, use sudo tee rather than sudo echo > because redirection happens in the current shell before sudo.
In pipelines with set -o pipefail, tee failures can go unnoticed. Use --output-error=exit for strict error handling.
When combined with commands that buffer output, you may not see immediate results. Use stdbuf or command-specific unbuffering options if needed.

SEE ALSO

cat(1), script(1), pv(1), sponge(1)

Copied to clipboard