tee command reads the standard input and writes it to both the standard output and one or more files. The command is named after the T-splitter used in plumbing. It basically breaks the output of a program so that it can be both displayed and saved in a file. It does both the tasks simultaneously, copies the result into the specified files or variables and also display the result.
In this tutorial, we will discuss the basics of the tee command using some easy to understand examples.
Tee Command Syntax
Before going into how to use the tee command, let’s start by reviewing the basic syntax:
tee [OPTIONS] [FILE]
- OPTIONS:
-a
(--append
) – Do not overwrite the files instead append to the given files.-i
(--ignore-interrupts
) – Ignore interrupt signals.- Use
tee --help
to view all available options.
FILE_NAMES
– One or more files. Each of which the output data being written to.
How to use Linux tee command
In the most basic usage, you would just pipe a command to tee and give it a filename. This will allow the command to still print to the screen, but also save the results in a file.
In the following example, we are using the df command
to get information about the amount of available disk space on the file system. The output is piped to the tee command, which displays the output to the terminal and writes the same information to the file disk_usage.txt.
df -h | tee disk_usage.txt
You can verify the content of the disk_usage.txt
file using the cat command
.
Write to Multiple File
[ads]
You can write the data from the tee command to multiple files at once. To do this you simple add a list of files after the tee command.
[box type=”note” align=”” class=”” width=””]Note:The multiple output files will have identical contents.[/box]
command | tee file1.out file2.out file3.out
For example:
ping google.com | tee output1.txt output2.txt output3.txt
Append to File
By default, the tee command overwrites information in a file when used again. However, if you want, you can change this behavior by using the -a
(--append
) command line option.
command | tee -a file.out
So basically, the -a
option forces tee to append information to the file.
Ignore Interrupt
The last option is ignore interrupts ( -i ). This tells tee to continue and exit gracefully if the user sends an interrupt (CTRL+C). There is no easy way to demonstrate this in a blog post, so here is an example of the syntax.
command | tee -i file.out
For example:
grep GET access.log | tee -i log_get.txt
I have never found a need for this, but I am guessing if you had a really long running command being piped to tee, it might be useful.
Hide the Output
If you don’t want tee to write to the standard output (display output on the screen), you just need to redirect it to /dev/null
:
command | tee file.out >/dev/null
Conclusion
You’ll likely agree now that tee is an extremely useful command. We’ve discussed it’s basic usage as well as majority of its command line options here. The tool doesn’t have a steep learning curve, so just practice all these examples, and you should be good to go. For more information, head to the tool’s man page.
If you have any question or feedback feel free to leave a comment.
Thanks for this great article.