askvity

How to use tee?

Published in Command Line Utility 5 mins read

The tee command is a powerful utility in command-line environments that allows you to read from standard input and write to standard output and specified files simultaneously. Think of it like a "T" junction pipe – data flows in, and it's split to go in two directions: to the screen (standard output) and into one or more files.

Using tee is straightforward, making it indispensable for tasks like monitoring output while saving it, or piping data to multiple commands or files at once.

Basic Syntax

The fundamental way to use tee is to pipe the output of another command into it. The syntax is:

command | tee [options] [file ...]

Let's break this down based on the reference:

  • command |: This represents the output of any command (like ls, echo, grep, etc.). The pipe (|) sends this output as standard input to the tee command.
  • tee: This is the command itself.
  • [options]: Here, you can specify flags to change how tee behaves.
  • [file ...]: This is where you name the file(s) to save the standard output. You can specify one or more files.

As stated in the reference: "tee – this command reads from standard input and writes to standard output and specified files. [options] – here, you can specify flags to change how tee behaves. For example, the -a flag appends output to a file instead of overwriting it. [file] – this is where you name the file(s) to save the standard output."

Key Options

The most common and useful option, mentioned in the reference, is the -a flag.

The -a Option (Append)

By default, tee will overwrite the content of the specified file(s) if they already exist. The -a option changes this behavior to append the new output to the end of the file(s) instead.

  • Without -a: command | tee output.txt (Overwrites output.txt)
  • With -a: command | tee -a output.txt (Appends to output.txt)

Practical Examples

Here are a few examples demonstrating how tee works in practice:

  1. Saving Command Output to a File While Seeing It on Screen:

    Let's say you want to list files in the current directory (ls -l) and save that list to a file named file_list.txt while also displaying it in your terminal.

    ls -l | tee file_list.txt

    After running this, the list of files will appear on your screen, and the same list will be saved in file_list.txt.

  2. Appending Output to an Existing File:

    Suppose you run the above command again, but you want to add the new listing after the previous one in file_list.txt. You would use the -a flag:

    ls -l | tee -a file_list.txt

    Now, the output is added to the end of file_list.txt instead of replacing its contents.

  3. Saving Output to Multiple Files Simultaneously:

    You can direct the output to more than one file at the same time.

    echo "This message goes everywhere!" | tee file1.txt file2.log file3.txt

    This command will print "This message goes everywhere!" to your screen and also write it into file1.txt, file2.log, and file3.txt.

  4. Using tee with Other Commands:

    tee is often used in complex pipelines. For instance, you might want to save the output of a command before filtering it further.

    cat big_log_file.log | tee original_log.txt | grep "ERROR" > errors_only.txt

    This command reads big_log_file.log, saves its entire content to original_log.txt (while also displaying it initially, though it's usually scrolled away for large files), and then the output is piped to grep "ERROR" which finds lines containing "ERROR" and saves those lines into errors_only.txt.

Summary Table

Component Description Example
command | Output from another command, piped into tee. ls -l |
tee The command that duplicates input to output and files. tee
[options] Flags modifying tee's behavior (e.g., -a for append). tee -a
[file ...] One or more files where the standard input will be written. tee file1.txt file2.log

tee is a fundamental tool for managing command-line output, providing a simple yet effective way to simultaneously view and record data streams.

Related Articles