Using the tee command, Linux creates the pos­si­bil­i­ty output standard input normally, and ad­di­tion­al­ly to write this to one or more files. This command is a useful solution to save in­ter­me­di­ate results, to archive them or to check for error sources.

What is the Linux tee command?

To better un­der­stand what the Linux tee command is and what it does, it is useful to look at its name. It gets its name from the T-piece of a wire that resembles a fork — and this is how the practical command works. On the one hand, it reads from the standard input “stdin”, which is used, for example, by the keyboard or a program, and outputs what it has read via the standard output “stdout”. In addition to the standard output, for example the screen, the tee command in Linux also writes the output to one or more files. Therefore, two or more outputs are created si­mul­ta­ne­ous­ly from the one input.

What is the function of the tee command in Linux?

The ad­di­tion­al file into which the Linux command tee saves the read data can be newly created if necessary. Al­ter­na­tive­ly, use an existing file whose contents are then deleted and rewritten. The most important reason for using the tee command is the ability to preserve in­ter­me­di­ate results within a Linux pipe. This way you can edit them later or check them for possible errors. Archiving is done parallel to the actual output without any technical re­stric­tions or hin­drances.

How is the command used?

The basic syntax of the Linux tee command is always the same and looks like this:

tee [OPTIONS] [FILE]
bash

It is important to always specify at least one file to write the output to.

A simple example for the use of the Linux command is the com­bi­na­tion with the command du (Disc Usage). This shows which part of a hard disk is occupied. The complete command then looks like this:

du -h | tee disk_usage.txt
bash

You get the in­for­ma­tion how much space is occupied on your hard disk. With the option “-h” (human readable) the used space is given in an un­der­stand­able format. At the same time as the standard output, the in­for­ma­tion is also stored and saved in the text file disk_usage.txt. If this does not exist yet, it will be created au­to­mat­i­cal­ly.

How do you prevent over­writ­ing the contents of a file?

If you do not take any further pre­cau­tions, the Linux-tee command will overwrite the in­for­ma­tion in a file as soon as it is used again and this file is specified. If you want to change this, simply use the -a command line option. This will make sure that the new content is added. In the example used above, it would look like this:

du -h | tee -a disk_usage.txt
bash

How do you write to multiple files at once with the Linux tee command?

It is possible to write the output to multiple files without much trouble. All you have to do is specify them one by one and separate them with spaces, as in the following example:

du -h | tee disk_usage1.txt disk_usage2.txt disk_usage3.txt
bash

How do you forward the output as input?

With the Linux tee command, however, you can not only write the output in parallel to any number of files. In addition, you also have the ability to forward the output as input to other commands. This works for example like this:

ls file* | tee third_file.txt | wc -m
bash

In this example, not only is the output stored in the document third_file.txt, but you also get in­for­ma­tion about how many char­ac­ters are in the file.

How do you use the Linux tee command with sudo?

You can also use the tee command in Linux together with sudo. This is ben­e­fi­cial or even necessary if you have written to a file that has root priv­i­leges. If you do not use sudo in such a case, you will only get an error message. Here is an example of the structure of this command:

echo “Example” | sudo tee -a root_document.txt
bash
Note

sudo is a command used in Unix-like operating systems like Linux or macOS to run programs with extended priv­i­leges. This is es­pe­cial­ly useful when per­form­ing tasks that normally fall under the ad­min­is­tra­tor’s to-dos.

How do you ignore an interrupt while executing tee?

The -i command line option gives you the option to ignore an interrupt while executing the tee command in Linux. The interrupt signal would otherwise be displayed when you press the key com­bi­na­tion [Ctrl] + [c] or [Ctrl] + [c]. You set the cor­re­spond­ing option directly after the tee command:

command | tee -i file.out
bash
Go to Main Menu