tee Cheatsheet
Quick reference for splitting command output to terminal and files with tee in Linux
The `tee` command reads from standard input and writes to standard output and one or more files at the same time. This cheatsheet covers overwrite and append modes, pipeline patterns, privileged writes, and practical logging examples.
Basic Syntax
Core tee command forms.
| Command | Description |
|---|---|
command | tee file.txt | Show output and write to a file |
command | tee -a file.txt | Show output and append to a file |
command | tee file1.txt file2.txt | Write output to multiple files |
command | tee | Pass output through unchanged |
command | tee /tmp/out.log >/dev/null | Write to file without terminal output |
Common Options
Frequently used flags for tee.
| Option | Description |
|---|---|
-a, --append | Append to files instead of overwriting |
-i, --ignore-interrupts | Ignore interrupt signals |
--help | Show help text |
--version | Show version information |
Logging Command Output
Capture output while still seeing it live.
| Command | Description |
|---|---|
ping -c 4 linuxize.com | tee ping.log | Save ping output to a log file |
journalctl -u nginx -n 50 | tee nginx.log | Save recent service logs |
ls -la | tee listing.txt | Save directory listing |
df -h | tee disk-usage.txt | Save filesystem usage report |
free -h | tee memory.txt | Save memory snapshot |
Append Mode
Keep history in log files with -a.
| Command | Description |
|---|---|
date | tee -a run.log | Append current date to log |
echo "deploy started" | tee -a deploy.log | Append status line |
./backup.sh 2>&1 | tee -a backup.log | Append stdout and stderr |
tail -n 20 app.log | tee -a diagnostics.log | Append recent log excerpt |
curl -I https://linuxize.com | tee -a headers.log | Append response headers |
Pipelines and Filters
Combine tee with text-processing commands.
| Command | Description |
|---|---|
cat app.log | tee copy.log | grep ERROR | Copy stream and filter errors |
ps aux | tee processes.txt | grep nginx | Save process list and filter |
sort users.txt | tee sorted-users.txt | uniq | Save sorted output and deduplicate |
dmesg | tee dmesg.txt | tail -n 30 | Save kernel messages and inspect recent lines |
find /etc -maxdepth 1 -type f | tee etc-files.txt | wc -l | Save file list and count lines |
Privileged Writes
Write to root-owned files safely.
| Command | Description |
|---|---|
echo "127.0.0.1 app.local" | sudo tee -a /etc/hosts | Append host mapping as root |
printf "key=value\n" | sudo tee /etc/myapp.conf >/dev/null | Overwrite config file as root |
cat config.conf | sudo tee /etc/myapp/config.conf >/dev/null | Copy config into protected path |
echo "net.ipv4.ip_forward=1" | sudo tee -a /etc/sysctl.conf | Append kernel setting |
sudo sysctl -p | tee sysctl-apply.log | Save reload output |
Troubleshooting
Quick checks for common tee issues.
| Issue | Check |
|---|---|
Permission denied | Use sudo tee for root-owned targets instead of sudo echo ... > file |
| File content replaced unexpectedly | Use -a when you need append mode |
| No output on terminal | Remove >/dev/null if you want to see output |
| Missing errors in logs | Redirect stderr too: 2>&1 | tee file.log |
| Command hangs in pipeline | Check whether the upstream command runs continuously and needs manual stop |
Related Guides
Use these guides for deeper command coverage and workflow patterns.
| Guide | Description |
|---|---|
| tee Command in Linux | Full tee command tutorial |
| grep Command in Linux | Filter matching lines |
| sort Command in Linux | Sort text output |
| tail Command in Linux | Inspect and follow recent lines |
| head Command in Linux | Show first lines quickly |
| journalctl Command in Linux | Query and filter systemd logs |
| Bash Append to File | Append redirection patterns |