Introduction
When the user executes a command in a Linux interactive shell, the output is displayed in the text terminal. However, there are ways to change this behavior by using additional shell commands connected in a pipeline.
In this tutorial, you will learn how to use the tee command on Linux to manage the output of a command.
Prerequisites
- A system running
- Command line or terminal access
- Sudo privilegesWhat
Linux
does the tee command do on Linux? The
tee command reads the standard input (stdin) and writes it to both the standard output (stdout) and one or more files. Tee is usually part of a pipeline, and any number of commands can precede or follow it.
Tee commands
on Linux with examples
The tee command is used alone or with additional options. The following sections list the available options and provide examples of using commands.
Basic usage
The basic syntax of the tee command is:
[command] | tee [options] [file name] The following example shows the use of tee to create a file that
stores information about a network interface while providing the same output in the terminal: The cat command confirms that tee correctly wrote the output of ifconfig in the file example.txt: If the file used for the command
already exists, Tee
Overwrites the previous contents of the file.
Append to given file
Overwriting file contents is the default behavior of the tee command. Use the –a (or -append) argument to add the command output to the end of the file.
[command] | tee -a [file name]
For example, use the echo command to add a line of text to a
file: Confirm the
correct addition with the cat command
:
Write to multiple
files
Use tee followed by any number of files to write the same output to each file:
[command] | tee [options] [filename1] [filename2]… The following example shows how to write the output of the echo command to two files: The ls command shows that tee
successfully created the example1.txt and example2.txt files. Hide the output To instruct tee to store
the output of the
command in a file and ignore the output of the terminal, use the following syntax:
[command] |
tee [options] [file name] >/dev/null In the following example, tee
creates a file that contains the network interface data, omitting the standard output: Redirecting the 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 in example.txt and passes the contents of that file to the grep command, which searches for and displays all instances of the word “example”: Ignore interrupts To allow tee to exit successfully even after the previous command has been interrupted, add the -i argument (or –ignore-interrupts
):
[command] | tee -i [file name] The following example shows the tee write output from the ping command
and completing the action correctly even after interrupting ping with Ctrl + C: Using tee with Sudo To allow tee to write to a file owned by root or to a file belonging to another user, Place the sudo command just before tee
.
[command] | sudo tee [options] [file name]
The following example shows a failed attempt to write to the sudoex.txt root property. When the sudo command is added
, the operation completes: Using tee in Vim Editor If you open and edit a root-owned file in Vim without using the sudo command, attempting to save your changes fails:
To override this error, type the following
in Vim: :w !sudo
tee
%
After entering the password sudo, Vim displays a warning but writes the changes to the file.
Diagnose errors when writing to pipes that are not pipelines
To instruct tee to print an error message when the process fails
, use the -p: [command] | tee -p [file name]
The default action of tee -p is to exit and print the error message immediately after detecting the write error in a pipe. To change the behavior of the command on the write error, use the –output-error argument, followed by the mode that specifies the behavior: [command
] | tee -output-error=[mode] [filename]
There are four possible modes
: Warn: Diagnoses errors when writing to any output. Warn-NoPipe: Diagnoses write errors on any non-pipe output. Exit:
- Closes when writing errors to
- exit-nopipe: exits with write errors on any
- non-pipe output.
any output.
Using the tee command
with Bash Script
The tee command is often found in bash scripts. Consider the following example:
The above script prints the message “Hello World” and stores the output in a log file. When you run the script, a log file is created in the tmp folder.
The log contains the output of the script:
View log files
Writing the script output to a log file
is usually done with the > operator
: ./testbash.sh > testbash.log The above command creates a log file,
but does not write anything
to the standard output.
Use tee to create a log file and view the output in the terminal:
./testbash.sh | tee testbash.log
See Help
and version information View the current version of the tee command by typing: tee -version For instructions on the syntax of the tee command and the available arguments, use the help argument of the command: tee -help
Conclusion By
reading this tutorial, you learned how to use
the tee
command
in a pipeline to manage command output. The article also talked about the use of tee in bash scripts.
Read more about shell commands in this Linux command cheat sheet.