Home Linux Commands Linux Tee Command Tutorial With Examples For Beginners

Linux Tee Command Tutorial With Examples For Beginners

By Karthick
2.7K views

When working with the terminal in Linux, there are many command line utilities available to make your life easy. One such utility is Tee. In this brief guide, we will learn what is Tee and the usage of Tee command in Linux with practical examples.

Introduction to Tee command

The tee is a simple, command line utility that accepts input and writes the output to a file and standard output (i.e. terminal). Tee command will mostly be used in shell scripts when you want to redirect the output to multiple files and further process the output.

Tee command is part of the GNU coreutils, so it comes pre-installed with all Linux distributions.

To check if the tee command is available in your Linux distribution, run the following command:

$ which tee
/usr/bin/tee

To check the tee version, you can use the --version flag.

$ tee --version
tee (GNU coreutils) 8.32

Display tee command help

If you've never used tee command, it is a good practice to start with tee command help section.

You can access the man page for tee command to know more about this utility.

$ man tee

You can also pass the --help argument to tee command to display its help manual.

$ tee --help

Tee command syntax

The typical syntax of Tee command is given below:

tee [OPTION]… [FILE]…

It accepts flags and filename where the output will be redirected.

There are two important flags you will mostly use with the tee command.

  • -a, --append flag : Append to files.
  • -i, -ignore-interrupts flag : Ignore Interrupt signals.

Now allow me to show you a few examples of tee command in Linux.

Linux Tee command examples for beginners

As stated already, the tee command will accept input and print the output to the terminal as well as a file which is passed as an argument.

In the below example, the tee command takes input from the echo command and prints the output to the terminal as well as to hello.txt file.

$ echo "Welcome to OSTechNix" | tee hello.txt
Welcome to OSTechNix

Let us check the content of hello.txt file:

$ cat hello.txt 
Welcome to OSTechNix
Tee command usage example in Linux
Tee command usage example in Linux

Use tee command with Pipe operator

Tee command is not just to print standard input to the terminal, and also pipe it into another program for further processing.

Mostly tee command will be used in combination with pipe (|) operator in shell scripts.

Take a look at the below example.

$ echo "Hello world" | tee hello.txt | rev
dlrow olleH
$ cat hello.txt
Hello world

Let me explain what happens when you run the above command.

  • The echo command will send its output "Hello World" as input to the tee command through the pipe.
  • Tee command will store the output in the given file hello.txt.
  • Instead of printing the output to the terminal, which is the default behavior, the tee command pass "Hello World" string as the input to the rev command which prints the string in reverse order.

Here is another example.

The following command creates a directory called "ostechnix", counts the number of characters in "ostechnix" and prints "ostechnix" to the terminal:

$ echo "ostechnix" | tee >(xargs mkdir) >(wc -c)

Heads Up: You may be required to use sudo with the tee command if you create files in directories that you don’t have access to as a normal user.

Passing multiple files

Tee command also accepts multiple files as arguments and stores the output to each file.

$ echo "Welcome to OSTechNix" | tee hello1.txt hello2.txt hello3.txt
Welcome to OSTechNix

Let us check the contents of each file:

$ cat hello1.txt
Welcome to OSTechNix
$ cat hello2.txt
Welcome to OSTechNix
$ cat hello3.txt
Welcome to OSTechNix
Send output to multiple files using tee command
Send output to multiple files using tee command

Override vs append

The default behavior of the tee command is it will search if the file is already available and override the data with the new one. If the file is not available it will create the file.

If you wish to append the data instead of overriding, use -a or --append flag.

$ cat hello1.txt
Welcome to OSTechNix
$ echo "I am using PoP_OS Cosmic Desktop" | tee -a hello1.txt
I am using PoP_OS Cosmic Desktop
$ cat hello1.txt
Welcome to OSTechNix
I am using PoP_OS Cosmic Desktop

Combining Tee with Redirection operator

If you wish not to print the output to the terminal but just write the output to files, you can then combine the redirection operator with the tee command.

This is an ideal case only if the tee command comes last in your pipe chain. If tee output is piped to another command like as
shown in the first example, no need of using the redirection operator.

$ echo "I am using PoP_OS Cosmic Desktop" | tee -a hello1.txt &> /dev/null

Interrupt signals

Tee command has the option to interrupt SIGINT using the -i or --ignore-interrupts flag.

Meaning - if you press CTRL + C in your terminal while you run tee command with -i flag, the tee command will interrupt the signal and do a graceful exit by reading EOF from the PIPE.

$ { echo Hello world;sleep 10; } | tee -i hello1.txt
Hello world
^C
$ cat hello1.txt
Hello world

Summary

Tee is a simple program that you may not use daily. However, knowing about tee command will come in handy when you write shell scripts. Hope this helps.

You May Also Like

Leave a Comment

* By using this form you agree with the storage and handling of your data by this website.

This site uses Akismet to reduce spam. Learn how your comment data is processed.

This website uses cookies to improve your experience. By using this site, we will assume that you're OK with it. Accept Read More