tee command in Linux with usage Examples

Last updated: November 23, 2022

The Linux tee command is one of the most commonly used commands in Linux distributions. In this tutorial, we will share with you the usage examples of the tee command, how it works, and some important uses of it.

tee command Linux

The Linux tee command is a line-oriented file copying and streaming utility. This command is named after the T-splitter used in plumbing, which divides water into two streams, one flowing to the tap and the other to the drain.

The Linux tee command is a powerful and useful command used to read from standard input and write the output to both standard output and one or more files. This can be very helpful when troubleshooting a program or process, as it can help to have a written record of what happened.

It is part of the GNU Coreutils package, like other commands such as cat and ls.

Syntax:

The tee command syntax is as follows:

tee [OPTIONS] [FILE]

tee Examples

Now that we've discussed how the command tee works let's see what it does in practice.

Display output and write to a file

Command tee read from standard input displays the standard output of a program and then writes it to a specified file system.

To demonstrate this, we'll use the free command. The free command displays your RAM usage, including used, free, shared, available memory, and swap space. We will use the free command with the -h option to make our output human-readable.

free -h

Our screen will look like the image below when we run the command above.

output of free -h command

Now, let’s use the command tee to output the free -h command results to the screen and save it to file1.txt.

free -h | tee file1.txt
output of free -h command write to a file using tee

We know you're wondering what just happened. Let's go over the image above. We piped the output of the free command to the tee command, and we included the name of the file we wanted the output to be written to.

The standard output was displayed...as well as saved in the file1.txt. We used the cat command to check the contents of the file1.txt file.

Overwrite to file - Append

What happens when we use the Linux tee command to write the output of another command to our existing file - file1.txt? Let’s find out!

We will use the df command for example. This command checks how much disk space is available on your file system. We will run this command with the -h option and pipe the output to the tee command. Finally, we will check the contents of file1.txt using the cat command.

Run the following command:

dh -f | tee file1.txt
tee overwrite to a file

From the image above, we can see that the original contents of the file1.txt file were overridden. This is what the command tee does by default.

We can change the way the previous command works with options. The first option on our list is append. We can use it like this:

free -h | tee -a file1.txt
tee overwrite to a file

What did we just do? The standard output of the free command was displayed, and at the same time, it was appended to file1.txt rather than overwriting the original contents of file1.txt. We then use the cat command to display the contents of file1.txt.

Write to Multiple Files

We can write to one or more files with the command tee by specifying a list of files we want to save the output to, separated by space.

For demonstration purposes, we'll use the free command. The basic syntax is as follows:

free -h | tee file1.txt file2.txt file3.txt file4.txt
tee write to multiple files

The image displayed the output from our command. We used the ls command to list the files in our home directory and the cat command to display the contents of each file.

Skip writing to standard output

With the tee command, it is possible to send the output of a command to a particular file without writing to the standard output (our computer screen). We only need to redirect the output to /dev/null. The /dev/null is a special file in every Linux file. It discards anything written to it.

Here is how to use the tee command for this purpose:

free -h | tee file1.txt > /dev/null
tee skip writing to output

From the image above, we can see that the free -h command output was printed to file1.txt and not to our screen.

Ignore Interrupts

We've seen the append option in action; now it's time to see the ignore interrupts option. In other words, with this option, tee ignores the interrupt signal usually sent by the terminal when you press CTRL + C.

Here's how to use the tee command with this option:

ping -D localhost 2>&1 | tee -i ping_result.log
tee ignore interrupts

We know the command above looks different. This time, we're using the ping command with the -D option to send an ICMP echo request to our localhost. Then we pipe both standard error and output to the tee command, just like we have been doing before.

And we use the -i option. The -i option prevents us from interrupting the program until it finishes running.

How to use tee with sudo permissions

Sometimes you'll want to write to a root-owned file as a sudo user, but the elevated privileges command will fail because sudo doesn't perform the output redirection.

Let's take a look at a file named practicefile.conf, owned by the root user. Using the cat command, we can display the content of this file because we have read permission. Now let's try to write to this file using the tee command.

We'll enter the code below in our command line:

sudo echo "add new line" | tee -a practicefile.conf
example of tee with sudo

As you can see, we couldn't write to the file because of the reason we stated earlier. How do we solve this problem? We simply prepend sudo before the tee command.

The command line code is as follows:

echo “add new line” | sudo tee -a practicefile.conf
using tee without sudo

The image above shows that "add new line" has been added to our practicefile.conf. tee received the output of the echo command, elevated to sudo permissions, and wrote to the file.

Using tee in combination with sudo command, allows us to write to files owned by other users.

using tee with shell script

The tee command isn't just for use in the terminal--it's also really helpful in bash scripts. Check out this bash script written using nano editor. This can also be done with vim editor.

example of tee using with shell script

Like every other bash script, we started with the shebang expression. We made a new variable and gave it a value. Lastly, we used the echo command to output a sentence and the tee command to append it to the variable we created.

When we ran the script, it created a new file in the /home/ubuntu directory. This new file had the output of the script inside of it.

Conclusion

It's up to you how you will use tee command. Using tee command can be helpful when analyzing a long output, like a log file. Saving outputs in a file is handy when you refer back to them later.

When you need some clarification about the tee command. We recommend checking out the man page; it always helps. To use the tee command with the man page, type the command man tee.

tee man page

Navigate all-in-one place of Linux Commands for more learning.

SHARE

Comments

Please add comments below to provide the author your ideas, appreciation and feedback.

Leave a Reply

Leave a Comment