Skip to main content

Module 3: Text Processing & Search

Lesson 34: tee Command

In this lesson, you'll learn how to use the tee command to read from standard input and write to standard output and files simultaneously in Linux.

The tee command reads from the standard input stream and writes it to the standard output stream and files.

tee Command Syntax

The syntax of the tee command is similar to other Linux commands. At a high level, it is divided into two groups - OPTIONS and FILES:

$ tee [OPTIONS] [FILE1] [FILE2] [FILE3] ...

In the above syntax, both OPTIONS and FILES are optional parameters.

tee Command Options

Option Description
-a Append the output to the file instead of overwriting it
-i Ignore the interrupt signal (SIGINT)

1. Save Output to a File

As discussed previously, the tee command sends output to the standard output as well as the file.

To understand this, first let's use the echo command to display the text on the standard output stream:

$ echo "tecmint.com"
tecmint.com

Now, let's use the tee command to write the output to the output.txt file:

$ echo "tecmint.com" | tee output.txt
tecmint.com

Finally, view the contents of the output.txt file using the cat command.

$ cat output.txt
tecmint.com

In the above output, we can see that the tee command sends output to the file as well as the standard output stream.

2. Append Output to a File

By default, the tee command overwrites the output files. However, we can avoid this by enabling the append mode, which appends the output at the end of the file:

$ echo "tecmint.com" | tee -a output.txt
$ cat output.txt
tecmint.com
tecmint.com

In this example, we have used the -a option to enable the append mode.

3. Write Output to Multiple Files

Pro TecMint Β· Root Plan
This Article is for Root Members
Join Root to read the full article and unlock everything

Full Access to Every Article, Course & Certification Track

Join thousands of Linux professionals who use Pro TecMint to advance their careers.

Ad-free access to all premium articles
All courses: Learn Linux, Bash, Golang, Ubuntu and more
RHCSA, RHCE, LFCS & LFCA certification prep
New courses added every month
Private Telegram community & priority support
Root Plan
$8/mo
or $59/year billed annually
Save $37 with annual plan
Start Reading This Article in the Next 60 Seconds
Join Root Plan β†’