How to Use the Linux tee Command

January 28, 2021


When the user executes a command in a Linux interactive shell, the output displays in the text terminal. However, there are ways to change this behavior using additional shell commands connected in a pipeline.

In this tutorial, you will learn how to use the tee command in Linux to manage the output of a command.

How to Use the Linux tee Command


  • A system running Linux
  • Access to the command line or terminal
  • Sudo privileges

What Does tee Command Do in Linux?

The tee command reads standard input (stdin) and writes it to both 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.

Graphical representation of the tee command operation

Note: To process standard inputs in Linux, you can use the xargs command which can be used in combination with other commands.

tee Commands in Linux With Examples

The tee command is used alone or with additional options. The following sections list the available options and provide command usage examples.

Basic Use

The basic syntax for the tee command is:

[command] | tee [options] [filename]

The example below demonstrates the use of tee to create a file that stores information about a network interface while providing the same output in the terminal:

Writing the output from ifconfig to a file using the tee command

The cat command confirms that tee successfully wrote the output of ifconfig to the file example.txt:

Confirming the success of the tee command using the cat command

If the file used for the command already exists, tee overwrites the previous contents of the file.

Append to the Given 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:

Appending the output of the echo command using the tee -a command

Confirm the successful addition with the cat command:

Using the cat command to confirm the successful addition of the output of the echo command to the file

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:

Adding the output of the echo command to two files using tee

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 the Output

To tell tee to store the command output in a file and skip the terminal output, use the following syntax:

[command] | tee [options] [filename] >/dev/null

In the example bellow, tee creates a file containing the network interface data, skipping the standard output:

Skipping the standard output and writing only to a file using tee

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”:

Passing the output of the ls command to the grep command using tee

Ignore Interrupts

To enable tee to exit properly even after the previous command has been interrupted, add the argument -i (or --ignore-interrupts):

[command] | tee -i [filename]

The next example shows tee writing output from the ping command and completing the action successfully even after ping is interrupted with Ctrl+C:

Successful completion of the tee command after the ping command is interrupted

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:

Using the sudo command to obtain write access for the tee command

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:

Attempting to save a root-owned file in Vim

To override this error, type the following into Vim:

:w !sudo tee %
Granting sudo permissions to tee in Vim

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 & Exit and How To Undo And Redo Changes In Vim.

Diagnose Errors Writing to Non-Pipes

To instruct tee to print an error message when the process fails, use the -p argument:

[command] | tee -p [filename]

The default action of tee -p is to exit and print the error message immediately upon detecting the error writing to a pipe. To change the command’s behavior on write error, use the --output-error argument, followed by the mode specifying the behavior:

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

There are four possible modes:

  • warn – diagnoses errors writing to any output.
  • warn-nopipe – diagnoses errors writing to any non-pipe output.
  • exit – exits on errors writing to any output.
  • exit-nopipe – exits on errors writing to any non-pipe output.

Use tee Command with Bash Script

The tee command is often found in bash scripts. Consider the following example:

Editing a bash script containing the tee command

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 folder. The log contains the output of the script:

Confirming the successful creation of the log file and checking its contents

Watch Log Files

Writing script output to a log file is usually performed with the >operator:

./ > 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:

./ | 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


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.

Was this article helpful?
Marko Aleksic
Marko Aleksić is a Technical Writer at phoenixNAP. His innate curiosity regarding all things IT, combined with over a decade long background in writing, teaching and working in IT-related fields, led him to technical writing, where he has an opportunity to employ his skills and make technology less daunting to everyone.
Next you should read
Vim Commands Cheat Sheet
July 22, 2020

Vim is an open-source text editor found in most Linux distributions. This article lists all the commonly used...
Read more
Linux Commands Cheat Sheet: With Examples
November 2, 2023

A list of all the important Linux commands in one place. Find the command you need, whenever you need it or...
Read more
How to Create a File in Linux Using Terminal/Command Line
July 11, 2024

Creating a file in Linux might seem straightforward, but there are some surprising...
Read more
How To Use grep Command In Linux/UNIX
February 29, 2024

This guide details the most useful grep commands for Linux / Unix systems. After going through...
Read more