Getting Started with the Tee Command
The tee
command is a powerful tool in the Linux terminal that allows you to redirect the output of a command to both the standard output (usually the terminal) and one or more files. This can be particularly useful when you need to save the output of a command for later reference or analysis while still displaying it in the terminal.
Understanding the Tee Command
The basic syntax of the tee
command is as follows:
command | tee file1 [file2 ...]
Here, the output of the command
is sent to both the standard output and the specified file(s). If you want to append the output to an existing file instead of overwriting it, you can use the -a
or --append
option:
command | tee -a file1 [file2 ...]
Practical Applications of the Tee Command
One common use case for the tee
command is when you need to monitor the output of a long-running command. By piping the output to tee
and a file, you can keep track of the command's progress and have a record of its output for later reference.
For example, let's say you want to monitor the output of the apt-get update
command on an Ubuntu 22.04 system:
sudo apt-get update | tee apt-get-update.log
This will display the output of the apt-get update
command in the terminal and also save it to the apt-get-update.log
file.
Another useful application of the tee
command is in shell scripting. You can use it to write the output of multiple commands to a single file, or to split the output of a command to multiple files for different purposes.
graph LR
A[Command] --> B[Tee]
B --> C[Standard Output]
B --> D[File1]
B --> E[File2]
By understanding the basics of the tee
command and its practical applications, you can become more efficient in your Linux terminal usage and shell scripting tasks.