tee Command in Linux

This tutorial explains Linux “tee” command, options and its usage with examples.

tee – Displays the output of a program and copies it into a file.

DESCRIPTION

The tee command reads standard input, then writes the output of a program to standard output and simultaneously copies it into the specified file or files.

SYNOPSIS

tee [ -a ] [ File … ]

OPTIONS

-a
Adds the output to the end of File instead of writing over it.

advertisement
advertisement

EXAMPLES

1. Write output to stdout, and also to a file

The following command displays output only on the screen (stdout).

$ ls

The following command writes the output only to the file and not to the screen.

$ ls > file

The following command (with the help of tee command) writes the output both to the screen (stdout) and to the file.

$ ls | tee file1.txt

2. You can instruct tee command to append to the file using the option –a as shown below

advertisement
$ ls | tee –a fileq.txt

3. You can also write the output to multiple files as shown below.

$ ls | tee file1 file2 file3

4. Write the output to two commands

You can also use tee command to store the output of a command to a file and redirect the same output as an input to another command.

advertisement

The following command will long list directory contents, store it in abc.txt and then count the no. of lines and echo it

$ ls -l | tee abc.txt | wc -l

Sanfoundry Global Education & Learning Series – 1000 Linux Tutorials.

If you wish to look at all Linux commands and their usage examples, go to Linux Commands Tutorial.

If you find any mistake above, kindly email to [email protected]

advertisement
advertisement
Subscribe to our Newsletters (Subject-wise). Participate in the Sanfoundry Certification contest to get free Certificate of Merit. Join our social networks below and stay updated with latest contests, videos, internships and jobs!

Youtube | Telegram | LinkedIn | Instagram | Facebook | Twitter | Pinterest
Manish Bhojasia - Founder & CTO at Sanfoundry
Manish Bhojasia, a technology veteran with 20+ years @ Cisco & Wipro, is Founder and CTO at Sanfoundry. He lives in Bangalore, and focuses on development of Linux Kernel, SAN Technologies, Advanced C, Data Structures & Alogrithms. Stay connected with him at LinkedIn.

Subscribe to his free Masterclasses at Youtube & discussions at Telegram SanfoundryClasses.