Introduction
When a user executes a command in a Linux interactive shell, the command prints the output to the terminal's standard output (stdout) stream. However, shell redirection and piping operators can redirect or pipe this output according to the user's needs.
In this tutorial, you will learn how to use the tee command in Linux to manage the output of a command.
Prerequisites
- A system running Linux.
- Command-line access.
- A user account with sudo privileges.
Linux tee Command Syntax
The basic syntax for the tee
command is:
[command] | tee [options] [filename]
The example below uses tee
to create an example.txt file that stores information about an enp0s3 network interface.
ifconfig enp0s3 | tee example.txt
The same information is also shown in the terminal output.
The following cat command confirms that tee
successfully wrote the output of ifconfig
to example.txt:
cat example.txt
Warning: If the file used in the command already exists, tee
overwrites the previous contents of the file.
tee Command Options
Below is the list of the options available to use with the tee
command:
Option | Description |
---|---|
-a , --append | Do not overwrite a file if it exists already. Append the new content instead. |
-i , --ignore-interrupts | Ignore interrupt signals. |
-p , <b>--output-error</b> [=MODE ] | Set write error behaviors. The following are the available modes:warn - diagnose errors.warn-nopipe - diagnose errors when writing to non-pipe outputs. The default mode when another mode is not specified.exit - exit on errors.exit-nopipe - exit on errors when writing to non-pipe outputs. |
--help | Print the help manual. |
--version | Show the command version. |
tee Command and Piping
The tee
command reads standard input (stdin) and writes it to both standard output (stdout) and one or more files.
Since its purpose is to forward a command output, tee
is always used as a part of a pipe. The common usage scenarios for the tee
command include capturing logs, debugging, and analyzing data streams.
Note: To process standard inputs in Linux, use the xargs command, which can be combined with other commands.
Linux tee Command Examples
The following sections provide examples of the most common uses of the tee
command.
Append to File
Overwriting the file's content is the default behavior of the tee
command. Use argument -a
(or --append
) to add the command output to the end of the file.
[command] | tee -a [filename]
For example, use the echo
command to append a line of text to a file:
echo "This text will be added" | tee -a example.txt
Confirm the successful addition with the cat
command:
Note: Learn more about various techniques for appending content to files using Bash.
Write to Multiple Files
Use tee
followed by any number of files to write the same output to each of them:
[command] | tee [options] [filename1] [filename2]...
The example below shows writing the output of the echo command to two files:
echo "Adding to multiple files" | tee example1.txt example2.txt
The ls command shows that tee
successfully created files example1.txt
and example2.txt
.
Note: Learn how to use tee command to write to file in Bash.
Hide Output
Use the following syntax to instruct tee
to store the command output in a file and skip the terminal output:
[command] | tee [options] [filename] >/dev/null
In the example below, tee
creates a file containing network interface data, skipping the standard output:
ifconfig enp0s3 | tee example.txt >/dev/null
Redirect Output of One Command to Another
tee
does not have to be the last command in the pipeline. Use it to forward the output to another command:
[command] | tee [options] [filename] | [command]
In the following example, tee
stores the output of the ls
command to example.txt and passes the content of that file to the grep command, which finds and displays all instances of the word "example":
ls | tee example.txt | grep "example"
Ignore Interrupts
To allow tee
to exit correctly even after the previous command has been interrupted, add the argument -i
(or --ignore-interrupts
):
[command] | tee -i [filename]
The following example shows tee
writing output from the ping command and completing the action successfully even after ping
is interrupted with ctrl+
c:
ping google.com | tee -i example.txt
Using tee with sudo
To enable tee
to write to a root-owned file or file belonging to another user, place the sudo command right before tee
.
[command] | sudo tee [options] [filename]
The example below shows an unsuccessful attempt to write to the root-owned sudoex.txt
. When the sudo
command is added, the operation completes:
echo "Added text" | tee -a sudoex.txt
Using tee in Vim Editor
If you open and edit a root-owned file in Vim without using the sudo
command, trying to save changes produces an error:
To override this error, type the following into Vim:
:w !sudo tee %
After you enter the sudo
password, Vim displays a warning but writes the changes to the file.
Note: To properly manage files in Vim, especially when executing changes, you need to know How to Save a File in Vim and Exit and How to Undo and Redo Changes in Vim.
Use tee Command with Bash Script
The tee
command is often found in bash scripts. Consider the following example of a script named testbash.sh:
#!bin/bash
LOGFILE=/tmp/log-files-$(date +%d%m%Y)
echo "Hello World" | tee -a $LOGFILE
The script above prints the "Hello World" message and stores the output in a log file. Executing the script creates a log file in the tmp
directory. The log contains the output of the script:
Watch Log Files
Writing script output to a log file is usually performed with the (>
) operator:
./testbash.sh > testbash.log
The command above creates a log file but does not write anything to standard output.
Use tee
to create a log file and see the output in the terminal:
./testbash.sh | tee testbash.log
See Help and Version Information
See the current version of the tee
command by typing:
tee --version
For the instructions regarding the tee
command syntax and the available arguments, use the command's help argument:
tee --help
Conclusion
By reading this tutorial, you learned how to use the tee
command in a pipeline to manage the command output. The article also talked about the use of tee
in bash scripts.
Read more about shell commands in this Linux commands cheat sheet.