tee - macOS


Overview

tee is a command-line utility in macOS that reads input from standard input and writes it to both standard output and one or more specified files. It is commonly used for logging input, duplicating output to multiple destinations, and processing data simultaneously.

Syntax

tee [options] [--] [file ...]

Options/Flags

  • -a, –append: Append to the specified files instead of overwriting them.
  • -i, –ignore-interrupts: Ignore interrupt signals (e.g., Ctrl-C) while writing to files.
  • -l, –line-buffered: Line buffer output, ensuring each line is written as it is encountered.
  • -u, –unordered: Unordered mode, where the order of output is not guaranteed. This can improve performance when writing to multiple files.

Examples

Simple Usage:

command | tee output.log

This sends the output of command to both standard output and output.log.

Appending to Files:

command | tee -a log1.txt log2.txt

This appends the output of command to log1.txt and log2.txt.

Line Buffering:

command | tee -l output.txt

This ensures that each line of output is written to output.txt as it is encountered.

Common Issues

  • Permission Denied Errors: Ensure you have appropriate permissions to write to the specified files.
  • Overwritten Files: Use the -a option to append to files instead of overwriting them.
  • Output Order Issues: In ordered mode (default), output can be interleaved between different destinations. Use the -u option for unordered mode.

Integration

tee can be integrated with other commands for advanced tasks:

  • Combine with cat: cat file1 file2 | tee merged.txt merges multiple files into merged.txt.
  • Use with grep: command | tee log.txt | grep error filters the output and logs only error messages.
  • Create Pipes: command | tee >(tee log1.txt >&2) creates a named pipe to simultaneously write output to log1.txt and standard error.
  • cat: Concatenate and display files.
  • head: Display the first part of a file.
  • dd: Copy and convert files or data.