There are times when you want to manually track output of a command and also simultaneously make sure the output is being written to a file so that yo
There are times when you want to manually track output of a command and also simultaneously make sure the output is being written to a file so that you can refer to it later. If you are looking for a Linux tool which can do this for you, you’ll be glad to know there exists a command tee that’s built for this purpose.
In this tutorial, we will discuss the basics of the tee command using some easy to understand examples. But before we do that, it’s worth mentioning that all examples used in this article have been tested on Ubuntu 16.04 LTS.
Linux tee command
The tee command basically reads from the standard input and writes to standard output and files. Following is the syntax of the command:
tee [OPTION]… [FILE]…
And here’s how the man page explains it:
Copy standard input to each FILE, and also to standard output.
The following Q&A-styled examples should give you a better idea on how the command works.
Q1. How to use tee command in Linux?
Suppose you are using the ping command for some reason.
And what you want, is that the output should also get written to a file in parallel. Then here’s where you can use the tee command.
ping google.com | tee output.txt
The following screenshot shows the output was written to the ‘output.txt’ file along with being written on stdout.
So that should clear the basic usage of tee.
Q2. How to make sure tee appends information in files?
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 command line option.
[command] | tee -a [file]
So basically, the -a option forces tee to append information to the file.
Q3. How to make tee write to multiple files?
That’s pretty easy. You just have to mention their names.
[command] | tee [file1] [file2] [file3]
ping google.com | tee output1.txt output2.txt output3.txt
Q4. How to make tee redirect output of one command to another?
You can not only use tee to simultaneously write output to files, but also to pass on the output as input to other commands. For example, the following command will not only store the filenames in ‘output.txt’ but also let you know – through wc – the number of entries in the output.txt file.
ls file* | tee output.txt | wc -l
Q5. How to write to a file with elevated privileges using tee?
Suppose you opened a file in the Vim editor, made a lot of changes, and then when you tried saving those changes, you got an error that made you realize that it’s a root-owned file, meaning you need to have sudo privileges to save these changes.
In scenarios like these, you can use tee to elevate privileges on the go.
:w !sudo tee %
The aforementioned command will ask you for root password, and then let you save the changes.
Q6. How to make tee ignore interrupt?
The -i command line option enables tee to ignore the interrupt signal (
SIGINT), which is usually issued when you press the crl+c key combination.
[command] | tee -i [file]
This is useful when you want to kill the command with ctrl+c but want tee to exit gracefully.
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.
Share this page: