The tee command in the Linux Shell/command line splits the output of an application – sending output to both a file and STDOUT (the console or another application). Here’s how to use it.
The tee command is named for a T-splitter used in plumbing – a pipe that redirects water from a single source in two directions.
tee Command Syntax
tee is a command with a simple purpose and simple syntax:
tee OPTIONS FILE
- OPTIONS is a list of options from the below table
- FILE is the path to the file you wish the output to be saved
- Input needs to be piped or redirected to tee – otherwise, it has nothing to work with
- This is done by redirecting standard input/output – see below for examples
- tee will output the data provided to it both to the FILE specified as well as STDOUT (standard output)
- By default, this will be output to the console, but it can also be redirected to other applications
Here are the most commonly used options for the tee command:
|-a, –append||Append to the given FILEs, do not overwrite|
|-i, –ignore-interrupts||Ignore interrupt signals|
For more options, including how to diagnose errors, you can view the user manual by running:
tee Command Examples
Here are some simple examples of how tee can be used.
All of these examples use the echo command, which simply outputs the text supplied to it.
View and Save the Output from a Command to a New File
The output of the echo command will be piped to tee, which will save it to a file as well as outputting to the console:
echo "hello!" | tee hello.txt
If the file hello.txt exists, it will be overwritten.
View and save the Output from a Command to an Existing File
This example does the same as above, but it will append to the end of an existing file rather than overwriting it:
echo "hello again!" | tee -a hello.txt
Save or Append Output to Multiple Files
Multiple files can be specified, separated by spaces:
echo "hello several files!" | tee hello1.txt hello2.txt hello3.txt
Redirect/Pipe tee Output
The standard output of tee can be piped and redirected. The below example will save the output of echo to hello.txt. The output will then be passed to the grep command rather than output to the console:
echo "hello!" | tee hello.txt | grep hello
For example’s sake, the grep command (which is used for searching text input) simply searches the output from tee for the word “hello.”
Ignoring interrupts (for example pressing CTRL + C to quit the command) can result in cleaner output from tee:
echo "hello!" | tee -i hello.txt
Using tee With sudo
The sudo command allows you to execute commands as the root user without logging in as root and is commonly used.
In the following snippet – sudo does not allow you to redirect the output of commands, as the sudo command itself does not perform the redirection as it appears before the redirection symbol (>):
sudo echo "hello!" > /root/hello.txt
…this will fail with a permission error because the /root directory requires root privileges, but the sudo command only applies these privileges to the echo command – not the redirection that comes afterward.
A trick to get around this is to use the tee command:
echo "hello!" | sudo tee /root/hello.txt
This will work because the output of the echo command is passed to tee, which is being run with administrative privileges via sudo – and tee saves the output.