When to use tee
Use tee to inspect command output while saving a copy. Be explicit about whether a report should replace an old file or append to it, and remember that shell privileges apply separately to each pipeline process.
Syntax
tee [OPTIONS] [FILE...]Square brackets mark optional arguments; an ellipsis means repetition. Replace example file names, user names and server addresses with your own values.
Common options and arguments
Usage examples
Save and show result
df -h | tee ./disk-report.txtThe disk report appears in the terminal and replaces the named report file.
Add entry
printf '%s\n' 'check complete' | tee -a ./checks.logThe check message is appended to the log without replacing existing lines.
Write one output to two files
printf '%s\n' 'ready' | tee ./one.txt ./two.txtThe same stream is written to two separate files and also displayed.
Notes and limitations
- By default, target files are zeroed; -a saves existing data.
- tee does not guarantee the success of the entire chain: in Bash, use pipefail if errors from previous commands are important.
Common questions and troubleshooting
Why does sudo before the first command not allow writing?
Each process has its own permissions. Elevating the producer does not elevate tee; grant only the permission needed by the writing process.
Can a report contain only part of a failing command's output?
Yes. A pipeline can create output before an upstream failure. Check relevant exit statuses and capture standard error separately when required.
Further help
Options vary by version and distribution. Check the installed documentation with this command: man tee.