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.
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.
We write tee commands using the following format:
$ tee [OPTION]... [FILE]...
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.
The basic usage of the tee command is as follows:
$ cat original.txt | tee copy.txt
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
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
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
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 22.214.171.124 | tee -i host.txt
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.
To solve this, we press escape key and write the following:
:w !sudo tee %
After this, errors will still be shown but our work will be saved to the file.
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]
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.