The tee command reads standard input and writes it both to standard output and to one or more files at the same time. For example, ls -l | tee listing.txt shows the listing on screen and saves a copy in listing.txt. It is named after the T-shaped pipe fitting that splits a flow in two directions.
Plain redirection with > sends output to a file and shows nothing on screen. The tee command fills that gap and also solves a classic problem with sudo and redirection, covered below.
In a pipeline, tee sits in the middle. Data comes in from the previous command, is copied to the named files and is passed on unchanged to the next command or the terminal.
The text is printed and also stored in deploy.log. Because tee passes data through, you can keep building the pipeline after it and save an intermediate result.
This saves the full process list, filters it for nginx and saves that result too, while the final output still appears on screen.
By default tee overwrites the file, like >. Use -a (or --append) to add to the end of the file, like >>.
Forgetting -a is a frequent mistake that silently destroys earlier log lines, so make it a habit for logs.
Give more than one file name and tee writes the same data to each of them.
The directory backup must already exist, because tee does not create directories. If one file cannot be opened, tee reports an error for that file and continues with the others, but it exits with a non-zero status.
A common surprise is this command failing.
The reason is that the shell performs the redirection before sudo runs, and the shell is running as your normal user. Only the echo gets elevated privileges. Run tee under sudo instead, so the process that opens the file is the privileged one.
Now the file is opened by sudo tee, so it succeeds. Add > /dev/null at the end if you do not want the text echoed to the terminal.
Another way is sudo sh -c 'echo ... >> /etc/hosts', but the tee form is easier to quote and read.
The tee command copies only standard input. In a pipeline, standard error is not piped, so it skips tee and goes straight to the terminal. Redirect it into the pipe with 2>&1 when you want both streams saved.
In Bash you can use the shorter form ./build.sh |& tee build.log. This is the usual way to watch a long build while keeping a complete log for later.
With Bash process substitution, tee can feed the same data to other commands rather than files.
Here the same log is filtered and counted at once. The -i option makes tee ignore the interrupt signal (Ctrl+C), which keeps it writing until its input ends. The -p option, available in GNU tee, controls how write errors on pipes are handled; most users never need it.
| Option | Meaning |
|---|---|
-a, --append | Append to files instead of overwriting |
-i | Ignore the interrupt signal |
-p | Diagnose errors writing to non-pipes (GNU) |
--output-error=MODE | Choose how to handle write errors (GNU) |
A redirect with > sends the output only to a file. The tee command sends it to a file and also to standard output, so you see it and can continue the pipeline.
The redirection is done by your own shell before sudo runs. Pipe into sudo tee file so the privileged process opens the file.
Use tee -a file. Without -a, tee truncates the file first.
Only if they are piped in. Use 2>&1 | or |& in Bash so that standard error reaches tee.
The tee command duplicates a stream: one copy goes to files and the other continues to standard output. Use -a to append, list several files to save the same data in many places, use sudo tee to write protected files and use 2>&1 | tee to save both normal output and errors. It is the easiest way to watch a command and keep a log at the same time.