The tee Command
Ever want to see output on screen AND save it to a file? That's what tee does.
Basic Usage
The output appears on screen AND is written to the file.
Why "tee"?
It's named after a T-pipe in plumbing - it splits the flow in two directions:
┌──> screen (stdout)
stdin ─┤
└──> file
Append Mode
By default, tee overwrites. Use -a to append:
Multiple Files
In Pipelines
tee can be anywhere in a pipeline:
This saves intermediate results while continuing the pipeline.
Debug Pipelines
Insert tee debug.txt in a pipeline to capture intermediate output for debugging without breaking the flow.
With sudo
A common gotcha:
# This fails - redirection happens before sudo
sudo echo "text" > /etc/file # Permission denied
# This works - tee runs as root
echo "text" | sudo tee /etc/file
# Suppress stdout (write silently)
echo "text" | sudo tee /etc/file > /dev/null
Why This Works
> is handled by your shell (as regular user). tee is a command that can run with sudo. So sudo tee can write to protected files.
Real-World Examples
Watch and Log
Build with Logging
Download and Show Progress
Write System Config
Tee vs Redirection
| Want to... | Use |
|---|---|
| Save output only | command > file |
| See and save output | command | tee file |
| Append and see | command | tee -a file |
| Write to root-owned file | command | sudo tee file |
How do you append to a log file while also seeing the output?
Quick Reference
| Command | Effect |
|---|---|
tee file | Write to file and stdout |
tee -a file | Append to file and stdout |
tee f1 f2 | Write to multiple files |
sudo tee file | Write to root-owned file |
tee file > /dev/null | Write to file only (suppress stdout) |
Key Takeaways
teesplits output to screen and file-aappends instead of overwriting- Use in pipelines to capture intermediate results
sudo teewrites to protected files when>can't- Name comes from T-shaped plumbing pipes
Congratulations! You've completed Chapter 5: Pipes and Redirection.
You now understand the core of Unix power - combining simple commands into complex workflows. This is the foundation everything else builds on.
Next chapter: File Permissions - understanding who can do what.