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 (likels
,echo
,grep
, etc.). The pipe (|
) sends this output as standard input to thetee
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
(Overwritesoutput.txt
) - With
-a
:command | tee -a output.txt
(Appends tooutput.txt
)
Practical Examples
Here are a few examples demonstrating how tee
works in practice:
-
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 namedfile_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
. -
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. -
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
, andfile3.txt
. -
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 tooriginal_log.txt
(while also displaying it initially, though it's usually scrolled away for large files), and then the output is piped togrep "ERROR"
which finds lines containing "ERROR" and saves those lines intoerrors_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.