We’ve all been there. You’re running a command, its output is flying by, and you desperately need to see it and save it to a file. Do you run it twice? Copy and paste from your terminal emulator? There’s a better way, and it’s called tee
.
The tee
command is a humble but incredibly powerful utility in the Unix-like operating system toolkit. Its name hints at its function: like a “T” junction in a pipe, it takes its standard input and simultaneously sends it to both standard output and one or more files.
Why is tee
so Useful?
Let’s dive into some scenarios where tee
truly shines:
- Simultaneous Viewing and Logging: This is
tee
‘s bread and butter. Imagine you’re compiling a large project or running a lengthy script. You want to monitor its progress in real-time on your screen, but you also need a complete log of the output for debugging or auditing later.
make | tee compilation_log.txt
In this example, the output of make
is piped to tee
. tee
then displays the output on your terminal and writes it to compilation_log.txt
.
- Debugging Pipelines: When you have a complex pipeline of commands, it can be tricky to see the output at intermediate stages.
tee
allows you to “tap into” the pipeline.
cat input.txt | grep "pattern" | tee intermediate_results.txt | sort
Here, you can see the results of the grep
command on your screen and in intermediate_results.txt
before they are passed to sort
.
- Appending to Files: By default,
tee
will overwrite the specified file. However, with the-a
(or--append
) option, you can append the output to an existing file. This is great for continuous logging.
./my_script.sh | tee -a daily_report.log
Each time my_script.sh
runs, its output will be added to the end of daily_report.log
without overwriting previous entries.
- Writing to Multiple Files: Need to send the same output to more than one file?
tee
handles that with ease.
ls -l | tee file_list.txt another_file.log
Now, both file_list.txt
and another_file.log
will contain the long listing of files in the current directory.
- Elevated Privileges (with
sudo
): A common frustration is trying to redirect output to a file that requires root privileges. A simplecommand > /path/to/file
will often fail because the redirection happens beforesudo
takes effect oncommand
.tee
comes to the rescue:
echo "Some configuration" | sudo tee /etc/some_config_file.conf
Here, echo
runs as your user, its output is piped to sudo tee
, and then tee
(running with root privileges) writes the content to the protected file.
Common tee
Options:
-a
,--append
: Append to the given files, do not overwrite.-i
,--ignore-interrupts
: Ignore interrupt signals (like Ctrl+C). This can be useful for ensuring logs are complete even if a process is interrupted.--help
: Display help and exit.--version
: Output version information and exit.
Beyond the Basics:
While tee
is conceptually simple, its versatility makes it an indispensable tool for system administrators, developers, and anyone who frequently works in the command line. It promotes efficient workflow by eliminating the need for repetitive commands or manual copy-pasting.
The next time you find yourself wishing you could see and save the output of a command, remember the mighty tee
. It’s a small command with a huge impact on your command-line productivity!