×

Search anything:

tee command in Linux

Internship at OpenGenus

Get this book -> Problems on Array: For Interviews and Competitive Programming

The tee command reads standard input and writes it to standard output and a file or multiple files. We use it in cases where we want to send the output of a command to a file and at the same time want to print it out to the standard output. Let's discuss it.

Table of contents.

  1. Introduction.
  2. Syntax.
  3. Usage.
  4. Summary.
  5. References.

Introduction.

The tee command reads standard input and can write it to a single file or multiple files including the standard output. Assume a scenario where we want to send the output of a command to a file and at the same time want to print it out to the standard output.

Normally, we would execute the command and use > or >> in case of appending then later cat the file. Using the tee command we can do both actions using a single command by piping the output of the first to the tee command which will add the content to the file we pass to it as an argument.

Syntax.

We write tee commands using the following format:

$ tee [OPTION]... [FILE]...

tee1

Commonly used options with this command include;

  • -a, this appends data to the specified file.
  • -i, to ignore interrupt signals.
  • -p, to examine errors writing to non-pipes.

Usage.

The basic usage of the tee command is as follows:

$ cat original.txt | tee copy.txt

tee2

In this example, we take the input of the cat command and pipe it to the tee command which creates a file teeFile.txt and adds the contents of file.txt.

We can also write to multiple files by passing the file names as arguments to the command:

$ cat original.txt | tee copy1.txt copy2.txt copy3.txt

tee3

Now all the files specified have similar contents

By default, this command will overwrite the existing contents of the file passed to it. To prevent this we can append to a file by using the -a option as follows:

$ cat original.txt | tee -a copy.txt

tee4

In this example, the contents of copy.txt are preserved and new content is appended to the file.

As we have seen, the tee command prints output to the terminal, we can hide this output by redirecting it to the /dev/null file, this file will discard anything written to it and return EOF when read:

$ cat original.txt | tee copy.txt > /dev/null

tee5

We use the -i option to ignore interrupts, that is, during execution, we may want to terminate execution using the CTRL + C keys, the -i option with tee command will allow it to exit gracefully:

$ ping -c 3 8.8.8.8 | tee -i host.txt

tee6

Now even if we interrupt the ping command, tee will exit properly

Some files in the Linux file system are privileged, that is, only users with appropriate permissions can read or write to them. We can use the sudo command with the tee to handle such situations. With sudo, our privileges will be elevated and we'll be able to perform any action to the privileged file.

$ [COMMAND] | sudo tee -a [PRIVILEDGED FILE] 

The tee command is also useful in situations where we opened a privileged file, wrote a lot of content, and on trying to write to the file and exit, we are presented with an error stating that we cannot make changes to the file since we did not open it with sudo privileges.
tee9

To solve this, we press escape key and write the following:

:w !sudo tee %

tee10

After this, errors will still be shown but our work will be saved to the file.
tee11

We use the -p option to print errors if a command fails. For example:

$ [command] | tee -p [filename]

By default, the above command exits and prints the error, we can change the default behavior by using the --output-error option followed by a specified behavior. This can be warn which diagnoses errors writing to any output, warn-nopipe which diagnoses errors writing to any non-pipe output, exit which exits errors are encountered writing to any output, and exit-nopipe which exits on errors writing to any non-pipe output.

[command] | tee --output-error=[mode] [filename]

Summary.

In Linux, there are cases where we want to execute a command and redirect its output to a file and at the same time display the output to standard output. Normally we would redirect the output using > or >> in case of appending then later concatenate the contents of the file we redirected output to. With tee command we perform these two actions all at once in a single command.

In this article at OpenGenus, we learned about this command (tee) through various examples.

tee command in Linux
Share this