Linux tee command explained (with examples)

s2 0

When you run basic commands on the terminal, the output is usually output to the terminal, that is, output by default. But what if you could also save the output to a file and print it to standard output? That’s what the tee command does. The Linux tee command reads from stdin (standard input) and then writes to stdout (standard output) and to one or more files.

Basic tee command syntax

The tee command uses the following syntax.

$ Command | tea [ options ] File (s)

Now let’s try some examples of using the tee command.

Basic usage of the tee command

Suppose we want memory and swap usage with the for free Command and save the output to a file named memory_usage.txt. The tee command would be called like this.

$ free -h | tee memory_usage.txt

The tee command reads from the command, saves the output to the memory_usage.txt File and then prints it to standard output.

To confirm that the output was saved in the memory_usage.txt File run the cat command as shown.

$ cat memory_usage.txt

Write the output to multiple files

In addition, you can save the output to multiple files as shown

$ Command | tea [ options ] File1 File2…

The following command uses the string “Hey, welcome to Linux” is saved in the two text files: File1.txt and file2.txt

$ echo Hey, Welcome to Linux | tee file1.txt file2.txt

Append content to a file

Typically, the tee command overwrites a file, and doing so is not always desirable because it can erase existing important data. Fortunately, you can -a Option to append text to a file. Let’s test this out.advertising

First we write the output of Operating time Command to the statistics.txt File as shown.

$ Operating time | t-Stats.txt

Use the cat Command to check this.

$ cat stats.txt

Next we will append the output of free -h Command that prints out our memory and swaps out the use of the file.

$ free -h | tee -a stats.txt

Double check the contents of the statistics.txt File. This time the file contains the output of the two commands as shown in the screenshot below. This is because we appended the output from free -h Command to the statistics.txt File and therefore the existing text was not affected.

Append content to file

Suppress the tee command output

If you don’t want the output of the tea Command printed in standard output, you can redirect it to / dev / null This is a special device that discards information.

Take the example below where we are printing the output of the df -Th Command in the text file, but suppresses the output on the terminal.

$ df -Th | tee disk_usage.txt >/dev/null

Suppress the tee command output

Let the tee interrupts command ignore

Sometimes you may want to stop a command that is running continuously. In this case, you should properly exit the tee command even after the program is interrupted. Use the -I or – Ignore interruptions Option provided in the syntax shown.

$ Command | tee -i filename

The following ping command continuously sends ping requests to Google’s DNS ( We interrupted the command after 4 consecutive pings. For the tea Command to gracefully exit, call the. on -I Opportunity.

$ ping | tee -i ping_stats.txt

Ignore interruptions

Without that -I Option the output and summary of the statistics is not printed.


Use the tee command with sudo

If you change files owned by the root user or any other login user, simply using the tee command without invoking the sudo command will result in an error.

In the following example we are creating a new repository named anydesk-stable.list for AnyDesk application in /etc/apt/sources.list.d Path reserved for the root user.

$ echo "deb all main" | tee /etc/apt/sources.list.d/anydesk-stable.list

As expected, the “Permissions Denied” error occurred because we do not have the permissions to create or modify a file in this path.

Use the tee command with sudo

The solution is before tea using the sudo command as shown.

$ echo "deb all main" | sudo tee /etc/apt/sources.list.d/anydesk-stable.list

On this occasion, the command is a success after placing sudo in front of the tee.

sudo tee command

Get help with the tea command

For more command options and assistance with using the tee command, run the following command.

$ tee --help

Help options for the tee command

Also, explore the man pages as shown

$ man tee

tee command man page

To check the version, do the following:

$ tee --version

Check tee command version


Everything here revolves around the Linux tee command. In particular, the command reads from standard input (stdin) and then writes to standard output (stdout) and the file (s).

Linux tee command explained (with examples)