How to Use tee Command in Linux

Wondering how to use the tee command in Linux? Don’t worry we got you covered!

Being Linux users, we seldom encounter days when we don’t have to use any sort of commands. In fact, your system can’t operate at all without commands being fed to it. Our computer is an operating system executing thousands of commands to meet our daily demands.

Similarly, we may encounter situations where we have to send our input across multiple streams instead of the usual linear fashion. Maybe we may need to send specific input over certain channels and output at the same time. In such cases, the linear Input/Output function does not effectively facilitate us. This is where the tee command comes into play.

This command effectively duplicates the desired portion of our data and sends it to the output and the desired file, thereby providing an additional path to input via file(s). Because of its “streaming action”, it is used in filters as well as pipes. In this case, you can say that the fluid is the input flow to stdout and the files we want to store our input in.

Now that we have sufficient information, let us learn how to use the tee command in Linux.

Understanding the tee Command.

tee is a very useful command, which helps in splitting data so we can get various versions of output at a certain point through different files. It is used to copy intermediate output to a file or command in a program.

Due to its significance, it is regarded as a basic command and is available on Unix and Linux OS, DOS, Windows, etc. It has also been ported to the IBM OS.

The tee command provides an additional pathway to the intermediate command to be used for execution. Hence it saves a lot of time and storage. Its work is similar to what is shown in the image below. 

tee Command in Linux

A good example would be imagining yourself using the command “add” to add two integers and two floating numbers. Now, you can open two Terminal windows to perform two tasks simultaneously, or you can do them in a single window back to back.

However, using the tee command, you can do both programs in a single file and change it to your will, all while keeping only one window open.

Syntax.

The general syntax of tee command is:

$ tee <options> <files>  

Let’s take a look at a brief demonstration of the tee command:

The following command will display output only on the Terminal screen.

$ ls

And the following command will write output to a file:

$ ls > <file name>

For example:

$ ls > file1.txt

Now, typing the following command will use a tee to duplicate output to file, as well as the standard output.

$ ls | tee <file>

We can also extend the functionality of the tee command to multiple files.

$ ls | tee <file1> <file2> <file3>

Without the tee command, you’ll get the following output:

tee command in linux

With tee command, it will write the contents in a .txt file along with standard output on the Terminal as shown below:

sample output
tee command in linux

With the syntax covered, let’s take a look at the options so you have a better understanding of how to use the tee command in Linux.

Options Description.

The tee command has various options to control operations of output and files into which data is being written. We now go over these options one by one.

a,--append: This option amalgamates the data on files to make a much bigger file. Usually, using the tee command for the same file overwrites data in that file. However, with the append option, we add to previous data without needing to overwrite.

To append, type the tee command with -a option as shown below:

$ ping google.com | tee -a ping.txt
tee command in linux
ping result
tee command in linux

-i,--ignore interrupts: This option is used when we want to ignore any interrupts issued by our command or program.

An interrupt is a forceful halt that may be caused by the user or the program itself to indicate an error or warning at some point of program execution. In this case, the ignore is interrupted and the command proceeds normally to write data on file and stdout.

p, modes: This option helps in error diagnostics which are written into the nonpipes. Using -p as an option causes the tee command to print an error in case the failure occurs.

In the default case, the tee command prints the error message. However, this order of handling can be changed by tweaking the mode, as discussed later.

--help for displaying help regarding command.

tee command in linux

--version for displaying the version.

version

Output Redirect to Another Command.

We can also use the tee command to redirect or forward the output of our file as input to other commands. For that, we type in the following set of commands in the Terminal window.

$ ls file* | tee.output.txt | wc -l

Or more generally.

$ <command1> | tee <option> <filename> | <command2>

In the above instruction, the “command2” parameter is the one to which output is being passed.

In general the tee command has the following syntax.

$ <command> | tee <options> <file>

Hide stdout While Writing to File.

Tee command can be made to hide output on the Terminal but still write to the file. This can be useful when you only want to have your command tree show up on the Terminal without any data whatsoever. 

You may type in this command:

$ <command> | tee <filename> >/dev/null

Which will conceal the output on the Terminal screen.

tee command in linux


Modes of the tee Command.

4 major modes of tee command are briefly described below:

  • Exit. In this mode, the tee command exits after the error occurs and writes to any output.
  • Exit no-pipe. Enables tee to exit after the error occurs, but writes to non-pipe output.
  • Warn. In this mode, tee may diagnose warnings and print to any output.
  • Warn no-pipe. In this mode, tee may diagnose for warning and print to non-pipe output.

Transferring control of Pipelines to users for efficiency of output and connecting multiple commands and options, referring to tee command as one of the core Terminal commands would be no exaggeration. 

If you would like to get an elaborated command map and instructions of the tee command for Linux, you can head to its mainpage.

Moreover, if you wish to know how to remove selective modules from your system, do check out how to use the rmmod command on Linux.

We hope this guide helped you learn how to use the tee command in Linux. If you have any questions or queries, let us know in the comment section down below.

If this guide helped you, please share it. 🙂

Leave a Reply

Your email address will not be published.

Previous Post

How to Change Your Location on Bumble

Next Post

How to Use curl command in Linux

Related Posts