🎁 New User? Get 20% off your first purchase with code NEWUSER20 Register Now →
Menu

Categories

tee Command

Intermediate Text Processing man(1)

Read from stdin and write to stdout and files

👁 10 views 📅 Updated: Mar 15, 2026
SYNTAX
tee [OPTION]... [FILE]...

What Does tee Do?

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.

Options & Flags

OptionDescriptionExample
-a Append to files instead of overwriting echo "new entry" | tee -a log.txt
-i Ignore interrupt signals (SIGINT) long_command | tee -i output.log
--output-error Set behavior on write error (warn, exit, etc.) command | tee --output-error=warn output.log
multiple files Write to multiple files simultaneously command | tee file1.txt file2.txt
/dev/null Discard stdout while keeping file output command | tee output.log > /dev/null
>(cmd) Process substitution — send to another command command | tee >(grep ERROR > errors.log)

Practical Examples

#1 Save and display output

Shows the directory listing on screen AND saves it to a file.
$ ls -la | tee directory_listing.txt

#2 Append to log file

Appends a timestamped message to the log file while displaying it.
$ echo "Deploy complete at $(date)" | tee -a deploy.log

#3 Write to protected file with sudo

The standard way to append to root-owned files. sudo echo > file does NOT work.
$ echo '127.0.0.1 mysite.local' | sudo tee -a /etc/hosts

#4 Save intermediate pipeline result

Captures raw and filtered data at different stages of the pipeline.
$ cat access.log | tee raw.log | grep 'ERROR' | tee errors.log | wc -l
Output: 42

#5 Write to multiple files

Writes the same output to three files simultaneously.
$ date | tee file1.txt file2.txt file3.txt

#6 Log command output silently

Saves all output (including errors) to build.log without displaying on screen.
$ make build 2>&1 | tee build.log > /dev/null

Tips & Best Practices

sudo tee for root files: Use echo "content" | sudo tee /etc/file instead of sudo echo "content" > /etc/file. The redirect runs as your user, not root.
Capture stderr too: To capture both stdout and stderr: command 2>&1 | tee output.log. This redirects stderr to stdout before tee.
Overwrite by default: tee overwrites files by default. Always use -a (append) for log files to avoid losing previous content.

Frequently Asked Questions

How do I save command output to a file AND see it on screen?
Pipe through tee: command | tee output.txt. Use -a to append instead of overwrite.
Why use tee instead of redirection?
Redirection (>) sends output only to a file, not to the screen. tee sends to both. Also, sudo tee works for root-owned files while sudo > does not.
How do I write to a file as root?
Use echo 'content' | sudo tee /path/to/file. This is the correct way because the redirect in sudo echo > file runs with user permissions, not root.

Master Linux with Professional eBooks

Curated IT eBooks covering Linux, DevOps, Cloud, and more

Browse Books →