tee Command Cheat Sheet
tee reads from standard input and writes to both standard output and one or a more files. It's named after the "T" pipe junction used in plumbing.
Synopsis
command | tee [OPTION]... [FILE]...
Basic Usage
Save and View
Run a command, see the output, AND save it to a file.
ls -la | tee filelist.txt
Append Mode (-a)
Don't overwrite the file; append to it.
echo "Server restart: $(date)" | tee -a system.log
Advanced Tricks
Write to Protected Files (Sudo Hack)
You cannot do sudo command > /etc/file because the redirection > runs as your user, not root.
Use tee instead:
echo "127.0.0.1 my.site" | sudo tee -a /etc/hosts
Ignore Interrupts (-i)
Ignore interrupt signals (Ctrl+C).
long_running_command | tee -i log.txt
Pipe to Multiple Commands
Process the data in different ways by using process substitution.
echo "Hello" | tee >(md5sum) >(sha256sum) | wc -c
Debugging Pipelines
Insert tee in the middle of a pipe to inspect data at that stage.
cat raw.txt | tee stage1.log | grep "Error" | tee stage2.log | sort
Notes
- Stdout:
teealways passes the input to stdout, so the next command in the pipe receives it unmodified. - Stderr:
teeonly captures stdout. To capture stderr too, redirect it first:command 2>&1 | tee log.txt.