Linux Tee Command with Examples

Linux Tee Command with Examples

When working with the Linux terminal, you might need to execute a command to write some data to a file and still process its output further. One powerful utility that can help you achieve that is the tee command.

This post will give you a detailed guide about the tee command and show you some real-world examples of the tee command.

What is Tee Command?

The tee command is a Linux command that will copy standard input to both the standard output, usually your screen, and the specified file. That is useful for piping commands together and comes quite handy when you want to redirect output to single or multiple files and still process the output further. The tee command got its name from the T-splitter used by plumbers when connecting several water pipes.

This might sound a little complicated if you are not well-versed with the Linux terminal. Let’s try using a graphical demonstration.

word image 12

Let’s say we had a file called employees.txt. We want to sort this text file, write the output to a file, and show the total number of items on the terminal. The long method of doing this would be to use the two commands shown below:

sort employees.txt > sortedEmployeeFile.txt
wc -l sortedEmployeeFile.txt

However, we can simplify it by using the tee command below.

sort employees.txt | tee sortedEmployeeFile.txt | wc -l

word image 13

Installing Tee Command

The tee command is among the tools installed with the GNU Coreutils. Therefore, it comes pre-installed on any Linux distribution – you don’t need to install it manually. However, you can confirm whether the utility is available by running the command below

which tee

word image 14

To check the version of the tee command running on your system, run the command below.

tee --version

word image 15

Basic Syntax for Tee Command

The basic syntax for the tee command is as follows:

tee [OPTIONS]... [FILE]...

The [OPTIONS] here may include:

  • -a: Prevents tee command from overwriting an existing file but instead append the data.
  • --version: This option shows you the currently installed version of the tee command.
  • --help: This option lists all the options available for the tee command

Tee Command Applications.

Now that you have a good understanding of the Tee command, let’s look at some real-world examples where you can apply this powerful command-line utility.

1. Write the Output of Ping Command to a File

You must have come across the ping command, especially when diagnosing your network connection. Ping is used for testing the availability of a host on the network. For example, if you are configuring a server, you can check whether it’s online by executing a command like

ping [server-address]



word image 16

Now, if we want to write this output to a file while still displaying the response from the server, we can execute the command below.

ping | tee pingTest.txt

The image below shows the ping output written to a file as well as a stdout.

word image 17

2. Use Tee Command to Append Data to a File

By default, when you execute the tee command to write output to an existing file, it will automatically overwrite all the existing data. For example, tee will automatically overwrite the existing data if we run a new ping command with the same filename for writing the output. We will use the -a option as shown in the syntax below to prevent this.

ping -a [fileName]
ping | tee -a pintTest.txt

3. Write to Multiple Files Using Tee

This is pretty easy if you have used other Linux commands to manipulate multiple files. To write to several files using the tee command, we will use the syntax below.

[your command] | tee file1 file2 file3 file4

word image 18

4. Redirect Command Output to Another Command with Tee

Other than just writing output to a file, you can use the tee command to redirect the output of one command to another. Let’s use the example below.

We want to list all the files in the home directory using the ls command then count the total number of files present using the wc -l command. Instead of writing two different commands, we can use a one-liner command thanks to tee, as shown below.

ls ~ | tee output.txt | wc -l

word image 19

5. Ignore Interrupt when Executing Tee

To stop any command running in the terminal, we always use the Ctrl + C keyboard combination to interrupt the process. If you are running a crucial task with Tee, you can prevent keyboard interrupt (SIGINT) using the -i option.

Use the syntax below.

[command] | tee -i [file-name]

6. Hide Tee Output

There are times when you execute a command that prints so much unnecessary output in the terminal. Luckily, you can hide the output by redirecting it to the /dev/null. Use the syntax below.

[command] | tee output_file_name >/dev/null

In the image below, you can see we executed the ping command, but no output is printed in the terminal. However, it has all been written to the specified output file.

word image 20

7. Use Tee with Sudo

There are situations where you would want to write data to a file owned by the root user. Let’s call this file website.conf. If we execute the command below, we will get the error – -bash: website.conf: Permission denied.

echo "newline" > website.conf

word image 21

To solve this, we can use the Tee command to prepend sudo in our command, as shown below.

echo "newline" | tee -a website.conf

word image 22


Up to this point, I believe you now have a good understanding of the tee command and how you can use it to perform your day-to-day tasks. By practicing a few more examples on your system, you should be good to go. If you have any feedback or questions then please feel free to let us know in the comments and we’ll get back to you as soon as possible.

Notify of
Receive notifications when your comment receives a reply. (Optional)
Your username will link to your website. (Optional)

Inline Feedbacks
View all comments
You May Also Like