tee
tee reads from standard input and writes to both standard output and one or more files simultaneously. The name comes from the T-shaped pipe fitting. It is useful when you want to save a build log to a file while still seeing the output in the terminal.
Syntax
command | tee [options] [file...]
Options
| Option | Description |
|---|---|
| tee file | Writes standard input to a file and also passes it to standard output. |
| tee -a file | Appends to a file instead of overwriting it. |
| tee file1 file2 | Writes to multiple files at the same time. |
| tee /dev/null | Passes data through to standard output only, effectively the same as cat. |
Sample Code
The following examples demonstrate how tee works. Think of tee as a T-shaped pipe fitting that splits the data stream in two directions.
# Data flow: # command output → tee → saved to file # └→ standard output (to next command)
Writes standard input to a file while also passing it to standard output.
echo "Hello" | tee output.txt Hello cat output.txt Hello
Displays build output in the terminal while saving it to a log file. Using 2>&1 includes standard error as well.
make 2>&1 | tee build.log
The -a flag appends to the file instead of overwriting it. This example appends to a log file while filtering for only errors and warnings.
make 2>&1 | tee -a build.log | grep -E "error|warning"
Writes to multiple files at the same time.
echo "test data" | tee file1.txt file2.txt file3.txt test data
Use sudo tee to write to a file that requires root privileges. sudo echo > /etc/hosts fails because the shell runs the redirect outside of sudo's permissions, but piping through sudo tee writes the file correctly.
echo "127.0.0.1 myapp.local" | sudo tee -a /etc/hosts 127.0.0.1 myapp.local
Saves an intermediate result to a file mid-pipeline so you can inspect it later.
cat access.log | tee /tmp/debug.log | grep "ERROR" | wc -l 3
Notes
The sudo tee pattern is a common idiom for writing to files that require root privileges. sudo echo "..." > /etc/hosts fails because the shell executes the redirect without sudo's elevated permissions, but using sudo tee via a pipe writes the file correctly.
For more on pipes, see Pipe (|). For more on redirection, see Output Redirection (>).
If you find any errors or copyright issues, please contact us.