tee command in Linux with examples

Any Linux user that has spent enough time with the command line will eventually run into a scenario where they would like to redirect standard output and/or standard error to a file (or multiple files) as well as the terminal at the same time. For something so trivial, surely there’s a way to send our output to both places at the same time. And this is exactly what the tee command in Linux is used for.

The Linux Bash shell supports redirecting standard input and standard output to either a file or your terminal, but not both at the same time. If you are in a situation where such a feature would be useful, you can pipe the output of a command to tee.

In this guide, you’ll learn how to use the tee command in Linux through examples. Follow along below to see learn about the various options that you can use with this command.

In this tutorial you will learn:

  • How to use the tee command on Linux
tee command in Linux with examples
tee command in Linux with examples
Software Requirements and Linux Command Line Conventions
Category Requirements, Conventions or Software Version Used
System Any Linux distro
Software tee
Other Privileged access to your Linux system as root or via the sudo command.
Conventions # – requires given linux commands to be executed with root privileges either directly as a root user or by use of sudo command
$ – requires given linux commands to be executed as a regular non-privileged user

Frequently Used Options

The tee command has a simple syntax and a very simple purpose, so it doesn’t take long to learn. The following examples will teach you everything you need to know about the tee command, so you’ll be able to redirect standard output and standard error to a file (or multiple files) and your terminal at the same time. You’ll also see how to append output to files in case you don’t wish to overwrite a file.

tee command examples showing how to redirect and append standard output to a file
tee command examples showing how to redirect and append standard output to a file

tee command in Linux Basic Examples

  1. Instead of using > for normal Bash redirection, you can use a pipe and the tee command without any extra options. You’ll just need to specify the name of the file you to which you want to write standard output. The output will then be shown in your terminal and will also be sent to the file.
    $ echo example output | tee file.txt
    

    The “example output” text was sent to our terminal as well as file.txt in this example.

  2. You can also send standard output to multiple files at the same time with tee. Simply specify the path to each file name in your command.
    $ echo example output | tee file1 file2 file3
    
  3. If you also want standard error to be redirected to the file, as opposed to only showing it in terminal, you can add the 2>&1 operator before the pipe to tee.
    $ echo example output 2>&1 | tee file.txt
    
  4. If you need to write output to a file owned by root or some other user on the system, note that you may find it necessary to prepend your tee command with sudo.
    $ echo example output | sudo tee /root/file.txt
    
NOTE
You can always use the man command to read more about the tee command and its official documentation. Click the previous link to see how to open the manual pages for any command on a Linux system.

Advanced Usage

The tee command is pretty simple, and you will probably find that you don’t usually supply any extra options when running the command. However, there are a few different options that can definitely come in handy in various situations. In this section of the guide, we’ll show you a few of the lesser known options of the tee command that we think are useful.

tee command in Linux Advanced Examples

  1. To append to a file instead of overwriting it, which is equivalent to the Bash >> operator, you can use the -a or --append option with tee.
    $ echo example output | tee -a file.txt
    
  2. Use the -i or --ignore-interrupts option to instruct tee to ignore interrupt signals. This will allow tee to exit more gracefully in the event that a user sends a sudden interrupt signal with Ctrl + C in terminal.
    $ ping linuxconfig.org | tee -i file.txt
    

Closing Thoughts

In this guide, we learned all about the tee command on Linux. The tee command is the only command you will need to know when you need to send standard output and/or standard error to multiple destinations at the same time. As you saw in the previous examples, tee can send command output to multiple files at the same time while still showing it in terminal as well.