๐ŸŽ New User? Get 20% off your first purchase with code NEWUSER20 ยท โšก Instant download ยท ๐Ÿ”’ Secure checkout Register Now โ†’
Menu

Categories

๐Ÿ’ก Text Processing August 5, 2026 9

Linux Command: tee

Read from stdin and write to stdout and files

Terminal โ€” Text Processing
Command
$ ls -la | tee directory_listing.txt

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.txt

Append to log file

echo "Deploy complete at $(date)" | tee -a deploy.log

Write to protected file with sudo

echo '127.0.0.1 mysite.local' | sudo tee -a /etc/hosts

Pro 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 โ†’

Share this tip