Tee Command Usage Examples

by Ramesh

Tee command is used to store and view (both at the same time) the output of any other command.

Tee command writes to the STDOUT, and to a file at a time as shown in the examples below.

Example 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 file

Example 2: 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.

The following command will take a backup of the crontab entries, and pass the crontab entries as an input to sed command which will do the substituion. After the substitution, it will be added as a new cron job.

$ crontab -l | tee crontab-backup.txt | sed 's/old/new/' | crontab –

Misc Tee Command Operations

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

$ ls | tee –a file

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

$ ls | tee file1 file2 file3
1 loki March 13, 2010 at 7:44 am

a very good book , like it very much

2 asha July 29, 2011 at 5:28 am

i like this site! i want to know dat difference between stdout stdin

3 nikesh August 30, 2011 at 8:20 am

tee is very useful command and very few people really know about it.

4 sindhu September 2, 2011 at 7:12 am

excellent work and understandable to everyone.

5 Ambar Agrawal December 1, 2011 at 2:33 am

Kudos to author!!


6 abhijit jena January 18, 2012 at 11:22 pm

can you help me about the autosys

7 karin May 7, 2012 at 8:28 am

It doesn’t work!!!!

8 Neil Anghan July 29, 2012 at 11:48 pm


9 Sumit Chauhan September 3, 2012 at 11:01 pm

Your posts are really helpful.

10 simil September 7, 2012 at 6:45 pm

nice explanation

11 bala November 19, 2012 at 7:22 pm

excellent explanation.

12 ajay June 27, 2013 at 8:08 am

what is the output of who| tee file |sort in unix??

is it
a)op of who is saved in file and sorted op is displayed on screen
b)or some other

13 Pradip Sabhadiya July 2, 2013 at 7:36 pm

Than x for giving explanation I was searching this from many days..Nice explanation..

14 gurdeep August 8, 2013 at 8:42 am

nice one… Excellent

15 Shakthi September 11, 2013 at 9:30 pm

Thank you!
It helped.

16 harsha September 19, 2013 at 3:08 am


17 yamalaiah September 24, 2013 at 8:49 pm

hi Ajay the out put for
who| tee file |sort

root pts/0 2013-09-25 09:14 (
root pts/1 2013-09-25 08:34 (:0.0)
root pts/2 2013-09-25 08:35 (
root tty1 2013-09-25 08:24 (:0)

18 artickl September 27, 2013 at 9:34 pm

Very useful when you need to run something and write output to a file, which is not accessible under original user.

Such as:
cat apache.conf.example | sudo tee apache.conf

19 artickl September 27, 2013 at 9:36 pm


If you write already sorted output, than do:
who| sort | tee file

who | tee file | sort

Will run “who” first, write the output to the file and only after that sort it, before show in on a screen.

20 V September 28, 2013 at 8:29 am

karin May 7, 2012 at 8:28 am

said “It doesn’t work!!!!”

I guess karin means the appending to a file with the -a option. Now if you copy the command from this page
ls | tee –a file
it doesn’t.
But try typing it in a terminal and it works;)

The dash –a in the posted example is not the correct dash that bash would accept: -a.

21 rohit uppal October 8, 2013 at 10:07 pm

Simple & effective.

22 Sampath Kumar January 15, 2014 at 8:42 pm

How to use this command for the binary with command line arguments

ex: ./a.out arg1 | tee log1

output is not visibile on the screen in this case.

23 Divya January 23, 2014 at 10:14 pm

very much informative..

24 Manoj J January 28, 2014 at 12:04 pm

nice work

25 vivek February 7, 2014 at 5:12 am

very informative

26 Bernie March 21, 2014 at 11:10 am

Good presentation, thanks.

27 Iseyas April 7, 2014 at 6:43 pm

SHort and descriptive, pls continue to post. hank you

Comments on this entry are closed.

Previous post:

Next post: