The tee Command in Linux: A Complete Introduction

The tee command is used to store and view the output of any command. In this article, we will go over some basic usage of this command. Before we begin, here’s an interesting fact about this command: it is named after the T-splitter frequently used in plumbing. With the help of the tee command, the user will find that they have to type less to get more work done.

Basic Usage of the tee command

The tee command basically breaks the output of a program into two flows, so that it can be both viewed as well as saved in a file. It does both tasks simultaneously: display the result on the screen as well as copy the output into an external file.

Unlike most commands, the tee command does not start off simply. What I mean by this is that you cannot just use the tee command as “tee filename.txt”, it’ll simply delete the entire file contents. What the users are supposed to do is use some command first and then pipe it into the tee command followed by the output file. An example would be:

echo "this is a simple example using the tee command" | tee out.txt

After executing the above command the user will be able to see both the output of the echo command in his/her stdout as well as check out the out.txt file created in the current directory. If you use the cat command and see the contents you will find it is simply the output that was shown to you in the stdout. This way the user can both see the stdout as well as save it in a file simultaneously. We can verify the result by simply running the cat command on the out.txt file:

Run the tee command as a pipe and with a file-name as argument
Run the tee command as a pipe and with a file name as an argument

You can run the following command to do the same thing but then you’ll be running two commands, and why run two when you can just run one?

Also read: Echo command in Linux

echo "this is a simple example using the tee command" > out.txt && cat out.txt

Appending to an existing tee command output file

The user can append to an already existing tee output file by using the -a flag. The resulting command will be as follows:

echo "This is a follow-up example of the tee command" | tee -a out.txt

The above code will result in a follow-up line in the out.txt we created previously.

Valuable use cases for the tee command

The tee command is not really known to too many Linux users, but it still holds its value in some of the following use cases. Let’s take a closer look.

Sometimes you may find while using vim that after opening up a file and doing some edits, you can’t write to the file as the file requires sudo privileges. We then helplessly exit out of vim, open it again with sudo this time and go about our ways. But this is where the tee command shines. We can execute the following command in vim to write to a file that requires sudo privileges:

:w !sudo tee %

If you hit enter then it’ll ask for the sudo password and after giving the password you’re good to go!

Another scenario is where you want to redirect some output to a protected file. Simply use the tee command as we used earlier in the first example, but give the tee command sudo privileges:

echo "this is a simple example using the tee command" | sudo tee protected_file.txt

Summary

So that’s it for the tee command. Hope you have found this article useful and are looking forward to more from Linux For Devices. Also, don’t forget to frequently consult the man pages to know more about any commands. To know more about Linux commands check out this link.