Linux tee command

Using the tee command, Linux creates the possibility output standard input normally, and additionally to write this to one or more files. This command is a useful solution to save intermediate results, to archive them or to check for error sources.

What is the Linux tee command?

To better understand 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 simultaneously from the one input.

What is the function of the tee command in Linux?

The additional file into which the Linux command tee saves the read data can be newly created if necessary. Alternatively, 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 intermediate 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 restrictions or hindrances.

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 combination 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 information how much space is occupied on your hard disk. With the option “-h” (human readable) the used space is given in an understandable format. At the same time as the standard output, the information is also stored and saved in the text file disk_usage.txt. If this does not exist yet, it will be created automatically.

How do you prevent overwriting the contents of a file?

If you do not take any further precautions, the Linux-tee command will overwrite the information 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 information about how many characters 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 beneficial or even necessary if you have written to a file that has root privileges. 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 privileges. This is especially useful when performing tasks that normally fall under the administrator’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 combination [Ctrl] + [c] or [Ctrl] + [c]. You set the corresponding option directly after the tee command:

command | tee -i file.out
bash
We use cookies on our website to provide you with the best possible user experience. By continuing to use our website or services, you agree to their use. More Information.