Linux Command: tee
Read from stdin and write to stdout and files
The tee command reads from standard input and writes to both standard output and one or more files simultaneously. Named after the T-splitter used in plumbing, it splits the output stream so you can save a copy while also passing data to the next command in a pipeline. tee is essential when you need to both see output on screen and save it to a file, or when you need to capture intermediate pipeline results without breaking the pipeline flow. Common use cases include logging command output while watching it in real-time, writing to files that require sudo permissions (via sudo tee), and creating pipeline taps for debugging complex data processing chains.
Syntax
tee [OPTION]... [FILE]...Key Options
-aโ Append to files instead of overwriting-iโ Ignore interrupt signals (SIGINT)--output-errorโ Set behavior on write error (warn, exit, etc.)multiple filesโ Write to multiple files simultaneously/dev/nullโ Discard stdout while keeping file output>(cmd)โ Process substitution โ send to another command
Examples
Save and display output
ls -la | tee directory_listing.txtAppend to log file
echo "Deploy complete at $(date)" | tee -a deploy.logWrite to protected file with sudo
echo '127.0.0.1 mysite.local' | sudo tee -a /etc/hostsPro Tips
- Use echo "content" | sudo tee /etc/file instead of sudo echo "content" > /etc/file. The redirect runs as your user, not root.
- To capture both stdout and stderr: command 2>&1 | tee output.log. This redirects stderr to stdout before tee.
- tee overwrites files by default. Always use -a (append) for log files to avoid losing previous content.
Learn more: Full tee reference โ