Shell
tee
Read standard input and write it to files and standard output.
pipeoutputfilesloggingredirect
Additional Notes
tee reads from standard input and writes the same data to standard output and to one or more files. It is the "T" junction of a pipeline: it lets you both display a command's output and save it at the same time.
A common trick is sudo tee FILE > /dev/null to write to a root-owned file from a normal user's pipeline, since > redirection happens before sudo applies.
Syntax
command | tee [options] [file ...]
Parameters
options: Flags that change howteebehaves.file: One or more files to write the input to.
Common Options
-a,--append: Append to files instead of overwriting them.-i,--ignore-interrupts: Ignore interrupt signals so a Ctrl-c does not stop the write.--output-error: Control behavior on write errors.-p: Diagnose errors writing to non-pipes.
Examples
ls -l | tee listing.txt
Print the listing and save a copy to listing.txt.
make 2>&1 | tee build.log
Save a build log while still watching output.
echo "config line" | sudo tee -a /etc/app.conf > /dev/null
Append a line to a root-owned file from a normal user's shell.
cat data.csv | tee part1.csv part2.csv
Write the same stream to two files and also show it.
./monitor.sh | tee -a run.log
Keep appending live output to a log file.
Practical Notes
- Without
-a,teeoverwrites the target file; use-ato keep history. tee > /dev/nulldiscards the screen copy and keeps only the file write.sudo teeis the standard way to redirect into privileged files from a pipeline.- Useful in long-running pipelines where you want both a live view and a saved record.