How to Use Tee on Linux
By: Zubair Ahmed
Tags: Command Line, Programming, Shell, Tee, Linux Tutorial

What Is Tee?

tee on Linux is a command-line tool, it reads from the standard input and is the capability of writing results to the terminal's standard output and files at the same time. This essentially lets you store and view (both at the same time) the output at any time.

How to Use It?

Let’s illustrate the use of tee with a simple example. We can use a command to ping [google.com](http://google.com)

The output should be displayed only on your screen which is standard output (STDOUT):

Now, let’s try and write the output to a file:

Now, your screen will pretty much be blank and the output will be writing to the file mention on the path ~/ping.log :



PING

64 bytes from 142.250.67.78: icmp_seq=0 ttl=116 time=12.489 ms

64 bytes from 142.250.67.78: icmp_seq=1 ttl=116 time=9.979 ms

64 bytes from 142.250.67.78: icmp_seq=2 ttl=116 time=10.664 ms

64 bytes from 142.250.67.78: icmp_seq=3 ttl=116 time=8.961 ms

64 bytes from 142.250.67.78: icmp_seq=4 ttl=116 time=9.497 ms

64 bytes from 142.250.67.78: icmp_seq=5 ttl=116 time=9.856 ms

64 bytes from 142.250.67.78: icmp_seq=6 ttl=116 time=10.083 ms

64 bytes from 142.250.67.78: icmp_seq=7 ttl=116 time=9.735 ms

64 bytes from 142.250.67.78: icmp_seq=8 ttl=116 time=9.066 ms # ~/ping.logPING google.com (142.250.67.78): 56 data bytes64 bytes from 142.250.67.78: icmp_seq=0 ttl=116 time=12.489 ms64 bytes from 142.250.67.78: icmp_seq=1 ttl=116 time=9.979 ms64 bytes from 142.250.67.78: icmp_seq=2 ttl=116 time=10.664 ms64 bytes from 142.250.67.78: icmp_seq=3 ttl=116 time=8.961 ms64 bytes from 142.250.67.78: icmp_seq=4 ttl=116 time=9.497 ms64 bytes from 142.250.67.78: icmp_seq=5 ttl=116 time=9.856 ms64 bytes from 142.250.67.78: icmp_seq=6 ttl=116 time=10.083 ms64 bytes from 142.250.67.78: icmp_seq=7 ttl=116 time=9.735 ms64 bytes from 142.250.67.78: icmp_seq=8 ttl=116 time=9.066 ms

Now, we will tee to do both at the same exact time:

The output this time around will be written to the screen as well as the file:



PING

64 bytes from 142.250.67.78: icmp_seq=0 ttl=116 time=9.486 ms

64 bytes from 142.250.67.78: icmp_seq=1 ttl=116 time=11.808 ms

64 bytes from 142.250.67.78: icmp_seq=2 ttl=116 time=9.171 ms

64 bytes from 142.250.67.78: icmp_seq=3 ttl=116 time=13.124 ms

64 bytes from 142.250.67.78: icmp_seq=4 ttl=116 time=9.766 ms

64 bytes from 142.250.67.78: icmp_seq=5 ttl=116 time=9.446 ms

64 bytes from 142.250.67.78: icmp_seq=6 ttl=116 time=11.303 ms

64 bytes from 142.250.67.78: icmp_seq=7 ttl=116 time=11.421 ms

64 bytes from 142.250.67.78: icmp_seq=8 ttl=116 time=9.201 ms

64 bytes from 142.250.67.78: icmp_seq=9 ttl=116 time=9.372 ms

64 bytes from 142.250.67.78: icmp_seq=10 ttl=116 time=10.267 ms # ~/ping.logPING google.com (142.250.67.78): 56 data bytes64 bytes from 142.250.67.78: icmp_seq=0 ttl=116 time=9.486 ms64 bytes from 142.250.67.78: icmp_seq=1 ttl=116 time=11.808 ms64 bytes from 142.250.67.78: icmp_seq=2 ttl=116 time=9.171 ms64 bytes from 142.250.67.78: icmp_seq=3 ttl=116 time=13.124 ms64 bytes from 142.250.67.78: icmp_seq=4 ttl=116 time=9.766 ms64 bytes from 142.250.67.78: icmp_seq=5 ttl=116 time=9.446 ms64 bytes from 142.250.67.78: icmp_seq=6 ttl=116 time=11.303 ms64 bytes from 142.250.67.78: icmp_seq=7 ttl=116 time=11.421 ms64 bytes from 142.250.67.78: icmp_seq=8 ttl=116 time=9.201 ms64 bytes from 142.250.67.78: icmp_seq=9 ttl=116 time=9.372 ms64 bytes from 142.250.67.78: icmp_seq=10 ttl=116 time=10.267 ms

So, essentially, the command has the following format:

tee [OPTION]... [FILE]...

And quite plainly the man page explains it as:

The tee utility copies standard input to standard output, making a copy in zero or more files. The output is unbuffered.

More Options

As you might have stumbled on the man page already, there are more options that can be fed to our tee command. Some of them are:

Append

By default tee command overwrites the file. You can append to the file by using the -a option as shown below:

Write to Multiple Files

You can write to multiple files by just specifying the names or directories of the files:

ping google.com | tee -a file1 file2 file3

Conclusion

I think we can all agree that tee is one delightful Linux command. The tool definitely is quite easy to use and has a little-to-no learning curve. But you can always view its man page at any time to know further information.

Read more at Grizzlybit