CIS120 Linux Fundamentals by Scott Shaper

The tee Command

Think of the tee command like a photocopier for your command output. Just like a photocopier can make a copy while keeping the original, tee lets you save a copy of your command's output to a file while still showing it on your screen. It's perfect for when you want to both see what's happening and keep a record of it!

Quick Reference

Command What It Does Common Use
tee Saves output to file while showing it on screen Logging, debugging, saving command output

When to Use tee

Use tee when you want to:

Common Options

Option What It Does When to Use It
-a Adds to end of file instead of overwriting When you want to keep existing content
-i Ignores Ctrl+C interruptions When you don't want to accidentally stop
-p Checks for writing errors When debugging file writing problems

Practical Examples

Basic Usage

# Save directory listing while showing it
ls -l | tee filelist.txt

# Save a message to a file while showing it
echo "Hello, World!" | tee greeting.txt

# Save to multiple files at once
echo "Important note" | tee note1.txt note2.txt

Real-World Scenarios

# Save installation output while watching it
make install | tee install_log.txt

# Keep adding to a log file
date | tee -a daily_log.txt
who | tee -a daily_log.txt

# Debug a command while saving output
ls -l /nonexistent 2>&1 | tee -i error_log.txt

Advanced Usage

# Save full output and filtered results
ls -l | tee full_list.txt | grep ".txt" > text_files.txt

# Create a backup while processing
cat original.txt | tee backup.txt | sort > sorted.txt

# Monitor system while logging
top -b -n 1 | tee system_snapshot.txt

Tips for Success

Common Mistakes to Avoid

Best Practices

Advanced Techniques

Multiple tee Usage

# Example 1: Save output at different stages
# This command:
# 1. Lists files (saved to stage1.txt)
# 2. Filters for .txt files (saved to stage2.txt)
# 3. Counts the lines (saved to stage3.txt)
ls -l | tee stage1.txt | grep ".txt" | tee stage2.txt | wc -l | tee stage3.txt

# Example 2: Save both original and filtered data
# This command:
# 1. Shows original file (saved to original.txt)
# 2. Finds error lines (saved to errors.txt)
# 3. Counts total error lines
cat data.txt | tee original.txt | grep "error" | tee errors.txt | wc -l

# Example 3: Create multiple backups
# This command:
# 1. Shows original file (saved to backup1.txt)
# 2. Sorts the data (saved to backup2.txt)
# 3. Removes duplicates (saved to final.txt)
cat file.txt | tee backup1.txt | sort | tee backup2.txt | uniq > final.txt

# Example 4: Back-to-back tee commands
# This command:
# 1. Shows file contents
# 2. Saves to backup.txt AND text.html at the same time
# Note: Both files get the exact same content
cat file.txt | tee backup.txt | tee text.html

# Example 5: Multiple back-to-back tee commands
# This command:
# 1. Shows file contents
# 2. Saves to three different files at once
cat file.txt | tee file1.txt | tee file2.txt | tee file3.txt

You can use tee as many times as you want in a pipeline. Each tee command:

  • Saves a copy of the data at that point in the pipeline
  • Passes the data along to the next command
  • Can save to one or more files

Think of it like taking snapshots at different stages of your work. Each tee command is like taking a picture of your data at that moment, while the data continues flowing to the next command.

You can even use tee commands back-to-back to save the same data to multiple files at once. Each tee command in the chain receives the exact same input and saves it to its file.

Complex Pipelines

# Save both normal and error output
command 2>&1 | tee output_and_errors.txt

# Process data while keeping original
cat data.txt | tee backup.txt | sort | uniq > unique.txt

# Monitor system while logging
top -b -n 1 | tee system_snapshot.txt

Debugging Scenarios

# Debug a script while running it
./myscript.sh 2>&1 | tee script_output.txt

# Monitor network while saving data
ping google.com | tee -a network_log.txt

# Track changes while processing
diff file1.txt file2.txt | tee changes.txt | wc -l