Problem Statement
Explain the tee command and its use cases. How does it help with logging and debugging pipelines?
Explanation
Tee reads from stdin and writes to both stdout and one or more files simultaneously, like a T-shaped pipe fitting. This allows capturing pipeline output to a file while still passing it to the next command. Basic usage: command | tee output.txt | next_command saves output to file and continues pipeline.
Use -a flag to append instead of overwrite: command | tee -a log.txt | next_command appends to log file. Multiple files: command | tee file1.txt file2.txt | next_command writes to multiple files. This is valuable for logging, debugging, and creating backups of intermediate pipeline results.
Debugging pipelines: cat data.csv | tee step1.txt | grep 'active' | tee step2.txt | sort saves intermediate results at each step for inspection if pipeline fails. Logging with sudo: echo 'content' | sudo tee /root/file.txt writes to root-owned file (sudo doesn't work with output redirection but does with tee).
Practical examples: make 2>&1 | tee build.log saves build output while displaying it. tail -f /var/log/syslog | tee -a monitor.log | grep 'error' monitors logs, saves everything to file, but only displays errors. script -c 'command' | tee output.txt captures terminal session including timing. Understanding tee improves debugging, logging, and troubleshooting capabilities in complex command pipelines.
