The Linux tee command reads from standard input and writes to both standard output and one or more files simultaneously. You use it when you want to see a command’s output on your screen while also saving that same output to a file for later use. This article shows 15 practical tee examples, explains how the command works, and covers common pitfalls.
What Does the tee Command Do?
The name tee comes from the T-shaped pipe fitting in plumbing: one input, two outputs. In Unix-like systems, tee sits in a pipeline and duplicates the data flow. Its standard syntax is:
command | tee [options] file1 [file2 ...]
By default, tee overwrites the target file. If you want to append instead, use -a (or --append). The command is part of the GNU coreutils package, so it is available on virtually every Linux distribution, macOS, and Windows Subsystem for Linux (WSL).
1. Basic Output to Screen and File
The most common use is saving a command’s output while still seeing it:
ls -la | tee directory_listing.txt
This prints the full directory listing to the terminal and writes the exact same text to directory_listing.txt. The file is created if it does not exist; if it does, it is overwritten without warning.
2. Appending Instead of Overwriting
To add output to the end of an existing file without destroying its current contents, use the -a option:
echo "New entry" | tee -a log.txt
This is useful for logging scripts where you want to keep a running history. Without -a, each run would wipe the previous log.
3. Writing to Multiple Files
tee can write to several files at once. The output goes to all of them plus the screen:
date | tee file1.txt file2.txt file3.txt
All three files receive the same content. This is handy when you need the same data in different directories or with different names.
4. Using tee with sudo to Write Protected Files
A frequent problem: you run echo "config" > /etc/somefile and get Permission denied because the shell opens the file before sudo runs. The solution is to pipe through tee with sudo:
echo "nameserver 8.8.8.8" | sudo tee /etc/resolv.conf
Here tee runs with root privileges and can write to the protected file, while the shell only needs permission to run the pipeline. This is the standard workaround for redirecting to system files.
5. Suppressing Screen Output with /dev/null
If you want to save output to a file but not see it on the terminal, send the screen copy to /dev/null:
find / -name "*.conf" 2>/dev/null | tee all_confs.txt > /dev/null
This writes the list to all_confs.txt but shows nothing on the screen. The 2>/dev/null part discards error messages from find.
6. Teeing Error Output
By default, tee only captures standard output (stdout). To also capture standard error (stderr), you must redirect it into stdout first:
command 2>&1 | tee output_and_errors.log
The 2>&1 sends error messages to the same pipe as normal output, so both appear in the log file. Without this, errors still show on screen but are not saved.
7. Using tee in a Pipeline to Preserve Intermediate Output
When you build a long pipeline, tee lets you inspect data at any stage:
cat access.log | tee raw.log | grep "404" | tee errors.log | sort | uniq -c
This saves the raw log, then the filtered 404 lines, and finally shows the sorted count. It is invaluable for debugging complex data processing.
8. Logging Script Output with Timestamps
In a shell script, you can log every command’s output while still showing it to the user:
#!/bin/bash
{
echo "Script started at $(date)"
df -h
uptime
} | tee script_log.txt
The block runs, output goes to both the terminal and the log file. This is a simple way to create audit trails.
9. Ignoring Interrupt Signals with -i
The -i (or --ignore-interrupts) option makes tee ignore keyboard interrupts (Ctrl+C). This is useful for long-running processes where you do not want a stray keystroke to kill the logging:
long_running_command | tee -i output.log
Note that the upstream command may still receive the interrupt; -i only affects tee itself.
10. Using tee with Process Substitution
In bash, you can use tee with process substitution to send output to another command instead of a file:
command | tee >(grep ERROR > errors.txt) | grep SUCCESS > successes.txt
This splits the output: one branch filters errors, the other filters successes, while the screen still shows everything. Process substitution (>(...)) is a bash feature, not POSIX.
11. Teeing Binary Data
tee works on raw bytes, so it can handle binary data. For example, copying a disk image while verifying it:
dd if=/dev/sda | tee backup.img | sha256sum
This writes the image to backup.img and simultaneously computes its checksum. The screen shows the hash, and you have both the backup and a way to verify it.
12. Common Mistakes and Pitfalls
- Forgetting
-awhen you need to append – you will silently overwrite the file. - Using
teewithout2>&1when you want errors logged – errors appear on screen but not in the file. - Piping to
teewithsudobut not the file path – the shell may still fail to open the file for reading. - Expecting
teeto preserve exit codes –teereturns its own exit status, not that of the upstream command. Useset -o pipefailin bash to get the first non-zero exit code.
13. Alternatives to tee
If you only need to save output and do not care about seeing it, simple redirection (> or >>) is enough. For logging with timestamps and rotation, tools like logger (syslog) or multitail offer more features. The script command records an entire terminal session, which is different from tee but sometimes confused with it.
Frequently Asked Questions
Does tee overwrite or append by default?
By default, tee overwrites the target file. Use -a to append. There is no interactive prompt before overwriting, so be careful when reusing filenames.
How do I use tee with sudo to write to a root-owned file?
Pipe your command into sudo tee /path/to/file. For example: echo "data" | sudo tee /etc/config.conf. This runs tee with root privileges, allowing it to write to protected paths.
Can tee capture both stdout and stderr?
Yes, but you must redirect stderr to stdout first with 2>&1. For example: command 2>&1 | tee log.txt. Without this, only stdout goes to the file.
What is the difference between tee and the > operator?
The > operator sends output only to a file; it does not display it on the screen. tee sends output to both the file and the terminal. Use > when you do not need to see the output, and tee when you want both.
Does tee work on macOS?
Yes, macOS includes tee as part of its BSD userland. The options are largely the same, though some GNU-specific flags like --output-error may not be available. Check man tee for your system.
Why does my pipeline exit code show an error when using tee?
Because tee returns its own exit status, which is usually 0. To get the exit code of the first failing command in a pipeline, use set -o pipefail in bash. This makes the pipeline return the rightmost non-zero exit status.
Related
Method: the standard formula for this calculation, rounded as each result states. See how the calculators work or report an error.
More cards in this drawer
- DDR4 RAM Prices Rising: Are These CPUs Still Worth Buying?
- Connect Mac to TV: Wired & Wireless Ways
- Over Ear vs On Ear Headphones: Key Differences
- Best Family Streaming Plans: Top Picks & Guide
- SSD Future: What Comes After NVMe?
- Mercedes SUVs You Should Buy Used