How to use the Tee command in Linux

If you've ever used pipe and redirection in Linux shell, sometimes you'll need to use the tee utility.

What does Tee hold?

A command like ls will display the contents of the current directory. In other words, it displays these content into a stdout (standard output), usually your screen, or more accurately a virtual terminal screen.

A command like ls > file123 will not display anything on the screen. That's because the > sign redirects all output to a file instead of displaying it as stdout. Now file123 will be filled with the contents shown earlier on the screen.

To display the contents of the folder on the screen and write it to a file, you must use two commands. But with a tee , you can do both at the same time.

 ls | tee file123 

Why should Tee be used when you can run a similar command twice?

In the above example, it is clear that you don't need a tee if you can execute ls normally, then redo and redirect the output to a file. However, you will encounter situations in which the output is unique. Imagine the situation you are trying to diagnose a problem. You run:

 diagnose | tee error.log 

The errors you get may be unique. You want them to be displayed on the screen so you can see what's going on when checking everything. But you also want those errors to be saved in a file, to review later or paste the output into a discussion forum and consult people about it.

Another common situation that you may need to tee is when you want to take the output of the command to a location that only root users can read or write. The following command does not work:

 /sbin/blkid > /root/somefile 

Then you might think, just use sudo ! But you will be surprised when this command is not working either:

 sudo blkid > /root/somefile 

That's because after sudo blkid executes, you are still logged in as not a root user. And your shell (usually bash) tries to write to / root / somefile with your regular user information. To solve this, you can use the tee :

 /sbin/blkid | sudo tee /root/somefile 

Append text and redirect errors

Tee is a simple but useful command. A command | tee somefile basic command | tee somefile usually sufficient for most situations. However, there are 2 cases where you will need these tips.

The first thing to know is a tee , by default, always overwrites a file. If you run:

 ls | tee somefile 

Then run:

 ls /tmp | tee somefile 

The second command will overwrite the content of somefile and you will only see the contents of the last command executed. To change this behavior, you can create text that links tee instead of overwriting. To do so, simply use the -a switch command.

 ls | tee -a somefile 

The second thing to know is that not all outputs are the same. Error messages are handled differently and although they appear on the screen, they are not considered stdout, but are considered stderr and not handled by tee . The following is an example of grep .

 grep -r L2TP /etc | tee somefile 

The results shown will look like the following image:

How to use the Tee command in Linux Picture 1

Notice Permission denied is written into stderr. The only thing written into stdout is the highlighted text. That's why you notice that the content of 'somefile' is what is shown in the image below:

How to use the Tee command in Linux Picture 2

In this case, grep is used to search for text and is useful when error messages are not redirected to the file. Error messages only fill the file with unnecessary information. You just want to see the results found. But when you need an error message, use 2>&1 , to redirect stderr to stdout.

 grep -r L2TP /etc 2>&1 | tee somefile 

With this command, you will notice that somefile now also contains error messages.

Hopefully, this guide includes everything you need to make the most of the tee . But if you encounter some difficult situation with a tee , leave comments in the comment section below for everyone to share.

Wish you use the successful tee command!

4.3 ★ | 3 Vote

May be interested

  • Instructions for using zforce command on LinuxInstructions for using zforce command on Linux
    the gzip command is a popular tool used to compress / decompress files in linux. tipsmake.com presents the basics of this tool in the article: some basic terminal commands in linux mint 11.
  • How to limit access to su command in LinuxHow to limit access to su command in Linux
    if you have added linux to your data center or are just using a single linux machine for your business, you need to make sure it is as secure as possible.
  • 12 things Linux is easier to do in the command line than graphical software12 things Linux is easier to do in the command line than graphical software
    graphical user interfaces (guis) were invented to make life easier for computer users, so it's very common to use them to perform most everyday tasks.
  • 11 df commands in Linux (with example)11 df commands in Linux (with example)
    with the df command in linux, you can easily check your system for details about disk space and file size. these 12 examples of df commands will show you how to do them in detail.
  • How to use the Linux command line on Android with TermuxHow to use the Linux command line on Android with Termux
    android is a very operating system 'capacity with more and more desktop accessibility applications. however, sometimes you want to make some things on android that can be as easy as desktop. fortunately, you can use the termux tool, which builds on the existing infrastructure and provides a command line environment that allows you to install real linux applications on your android device.
  • Instructions for using find command in LinuxInstructions for using find command in Linux
    the find command is one of the most important and handy commands on a linux system. as its name suggests, the command can find files on a linux pc based on a variety of conditions and variables you set.
  • How to use the history command in LinuxHow to use the history command in Linux
    as you spend more and more time in terminal sessions, you will constantly find new commands that make everyday tasks more efficient. the gnu history command is one of them.
  • Instructions for using pstree command on LinuxInstructions for using pstree command on Linux
    pstree is a powerful and useful command to display processes running in linux. like the ps command, it shows all the processes that are currently active on your login system. the main difference is that when running the pstree command, processes are organized into tree sorting instead of lists like using the ps command.
  • The dd command in Linux, How to use the dd commandThe dd command in Linux, How to use the dd command
    dd is a command line utility for unix-like and unix operating systems, with the main purpose of converting and copying files.
  • Use the Top command to see the processes running in LinuxUse the Top command to see the processes running in Linux
    the top command in linux is used to display all processes running in the linux environment. this tutorial shows you how to use the top command through explaining the various parameters available and the content they display.