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

  1. Save command output while viewing
  2. Log pipeline data
  3. Duplicate data streams
  4. Debug pipeline operations
  5. 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

  1. Log command output:

    make 2>&1 | tee build.log
  2. Monitor and save:

    tail -f /var/log/syslog | tee current.log
  3. Pipeline debugging:

    cat data.txt | process1 | tee intermediate.txt | process2

Advanced Usage

  1. Ignore interrupts:

    long_command | tee -i output.log
  2. Append to multiple files:

    echo "data" | tee -a log1.txt log2.txt log3.txt
  3. 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

  1. Use for important command logging
  2. Combine with error redirection
  3. Consider append vs overwrite
  4. Use with sudo for privileged writes
  5. Monitor disk space when logging

Integration Examples

  1. Build logging:

    ./configure && make 2>&1 | tee build.log
  2. System monitoring:

    vmstat 1 | tee -a system_stats.log
  3. 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

  1. Dual logging:

    exec > >(tee -a script.log)
    exec 2>&1
  2. Progress monitoring:

    long_process | tee >(wc -l > progress.txt)