What is tee
tee reads from stdin and writes to stdout and to one or more files simultaneously. It acts as a T-junction in a pipeline — the data flows through to the terminal while also being saved.
Basic usage
command | tee file.txt
The output appears on screen as usual, and is also written to file.txt.
Example — capture the output of free:
free | tee free.txt
You see the output on screen and free.txt contains the same content.
Append instead of overwrite
By default tee overwrites the file. Use -a to append:
command | tee -a log.txt
Useful for accumulating log entries across multiple runs.
Write to multiple files
command | tee file1.txt file2.txt
Both files receive the same output.
Pass output to another command
tee still writes to stdout, so you can keep piping:
command | tee file.txt | grep error
This saves the full output to file.txt while passing only matching lines to grep.
The sudo tee pattern
A very common situation: you want to write to a file owned by root, but your shell runs as a regular user. This fails:
sudo echo "text" > /etc/file.conf # WRONG — redirection runs as your user
The correct pattern is:
echo "text" | sudo tee /etc/file.conf
Or to append:
echo "text" | sudo tee -a /etc/file.conf
tee runs as root (via sudo) and handles the write, while echo runs as your user.
Suppress screen output
If you only want the file and not the terminal output, redirect stdout to /dev/null:
command | tee file.txt > /dev/null
Practical examples
Save a build log while watching it:
make 2>&1 | tee build.log
The 2>&1 merges stderr into stdout so both are captured.
Capture package installation output:
apt install nginx | sudo tee /var/log/nginx-install.log
Write a config block to a root-owned file:
echo "net.ipv4.ip_forward=1" | sudo tee -a /etc/sysctl.conf
sudo sysctl -p
See also
man tee — full reference.
- Created: May 22, 2007
- Last edited: July 7, 2026