Skip to main content

tee Cheatsheet

By Dejan Panovski Updated on Download PDF

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.

CommandDescription
command | tee file.txtShow output and write to a file
command | tee -a file.txtShow output and append to a file
command | tee file1.txt file2.txtWrite output to multiple files
command | teePass output through unchanged
command | tee /tmp/out.log >/dev/nullWrite to file without terminal output

Common Options

Frequently used flags for tee.

OptionDescription
-a, --appendAppend to files instead of overwriting
-i, --ignore-interruptsIgnore interrupt signals
--helpShow help text
--versionShow version information

Logging Command Output

Capture output while still seeing it live.

CommandDescription
ping -c 4 linuxize.com | tee ping.logSave ping output to a log file
journalctl -u nginx -n 50 | tee nginx.logSave recent service logs
ls -la | tee listing.txtSave directory listing
df -h | tee disk-usage.txtSave filesystem usage report
free -h | tee memory.txtSave memory snapshot

Append Mode

Keep history in log files with -a.

CommandDescription
date | tee -a run.logAppend current date to log
echo "deploy started" | tee -a deploy.logAppend status line
./backup.sh 2>&1 | tee -a backup.logAppend stdout and stderr
tail -n 20 app.log | tee -a diagnostics.logAppend recent log excerpt
curl -I https://linuxize.com | tee -a headers.logAppend response headers

Pipelines and Filters

Combine tee with text-processing commands.

CommandDescription
cat app.log | tee copy.log | grep ERRORCopy stream and filter errors
ps aux | tee processes.txt | grep nginxSave process list and filter
sort users.txt | tee sorted-users.txt | uniqSave sorted output and deduplicate
dmesg | tee dmesg.txt | tail -n 30Save kernel messages and inspect recent lines
find /etc -maxdepth 1 -type f | tee etc-files.txt | wc -lSave file list and count lines

Privileged Writes

Write to root-owned files safely.

CommandDescription
echo "127.0.0.1 app.local" | sudo tee -a /etc/hostsAppend host mapping as root
printf "key=value\n" | sudo tee /etc/myapp.conf >/dev/nullOverwrite config file as root
cat config.conf | sudo tee /etc/myapp/config.conf >/dev/nullCopy config into protected path
echo "net.ipv4.ip_forward=1" | sudo tee -a /etc/sysctl.confAppend kernel setting
sudo sysctl -p | tee sysctl-apply.logSave reload output

Troubleshooting

Quick checks for common tee issues.

IssueCheck
Permission deniedUse sudo tee for root-owned targets instead of sudo echo ... > file
File content replaced unexpectedlyUse -a when you need append mode
No output on terminalRemove >/dev/null if you want to see output
Missing errors in logsRedirect stderr too: 2>&1 | tee file.log
Command hangs in pipelineCheck whether the upstream command runs continuously and needs manual stop

Use these guides for deeper command coverage and workflow patterns.

GuideDescription
tee Command in LinuxFull tee command tutorial
grep Command in LinuxFilter matching lines
sort Command in LinuxSort text output
tail Command in LinuxInspect and follow recent lines
head Command in LinuxShow first lines quickly
journalctl Command in LinuxQuery and filter systemd logs
Bash Append to FileAppend redirection patterns