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
|Requirements, Conventions or Software Version Used
|Any Linux distro
|Privileged access to your Linux system as root or via the
|# – requires given linux commands to be executed with root privileges either directly as a root user or by use of
$ – 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 in Linux Basic Examples
- 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.txtin this example.
- 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
- 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>&1operator before the pipe to tee.
$ echo example output 2>&1 | tee file.txt
- 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
$ echo example output | sudo tee /root/file.txt
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.
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
- To append to a file instead of overwriting it, which is equivalent to the Bash
>>operator, you can use the
--appendoption with tee.
$ echo example output | tee -a file.txt
- Use the
--ignore-interruptsoption 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 + Cin terminal.
$ ping linuxconfig.org | tee -i file.txt
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.