tee
Overview
The tee
command reads from standard input and writes to both standard output and files simultaneously. It’s like a T-junction for data streams.
Syntax
tee [options] [file...]
Common Options
Option | Description |
---|---|
-a |
Append to files |
-i |
Ignore interrupt signals |
-p |
Diagnose errors writing to pipes |
Key Use Cases
- Save command output while viewing
- Log pipeline data
- Duplicate data streams
- Debug pipeline operations
- Create multiple output files
Examples with Explanations
Example 1: Basic Usage
ls -la | tee file_list.txt
Shows directory listing and saves to file
Example 2: Append Mode
date | tee -a log.txt
Adds timestamp to log file while displaying
Example 3: Multiple Files
ps aux | tee process1.txt process2.txt
Saves process list to multiple files
Common Usage Patterns
Log command output:
make 2>&1 | tee build.log
Monitor and save:
tail -f /var/log/syslog | tee current.log
Pipeline debugging:
cat data.txt | process1 | tee intermediate.txt | process2
Advanced Usage
Ignore interrupts:
long_command | tee -i output.log
Append to multiple files:
echo "data" | tee -a log1.txt log2.txt log3.txt
Combine with sudo:
echo "config" | sudo tee /etc/config.conf
Performance Analysis
- Minimal overhead
- Efficient for data duplication
- Good for pipeline operations
- Handles large data streams well
- Low memory usage
Best Practices
- Use for important command logging
- Combine with error redirection
- Consider append vs overwrite
- Use with sudo for privileged writes
- Monitor disk space when logging
Integration Examples
Build logging:
./configure && make 2>&1 | tee build.log
System monitoring:
vmstat 1 | tee -a system_stats.log
Backup with logging:
rsync -av /data/ /backup/ | tee backup.log
Sudo Integration
Write to protected files:
echo "new config" | sudo tee /etc/protected.conf > /dev/null
Pipeline Debugging
Insert tee to inspect data:
cat input.txt |
process1 |
tee debug1.txt |
process2 |
tee debug2.txt |
process3 > output.txt
Error Handling
Capture both stdout and stderr:
command 2>&1 | tee output.log
Scripting Applications
Dual logging:
exec > >(tee -a script.log) exec 2>&1
Progress monitoring:
long_process | tee >(wc -l > progress.txt)