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
Comments on this entry are closed.
a very good book , like it very much
i like this site! i want to know dat difference between stdout stdin
tee is very useful command and very few people really know about it.
excellent work and understandable to everyone.
Kudos to author!!
Excellent!!!
can you help me about the autosys
It doesn’t work!!!!
Excellent
Your posts are really helpful.
nice explanation
excellent explanation.
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
Than x for giving explanation I was searching this from many days..Nice explanation..
nice one… Excellent
Thank you!
It helped.
nice.
hi Ajay the out put for
who| tee file |sort
is
root pts/0 2013-09-25 09:14 (192.158.0.106)
root pts/1 2013-09-25 08:34 (:0.0)
root pts/2 2013-09-25 08:35 (192.158.0.110)
root tty1 2013-09-25 08:24 (:0)
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
@yamalaiah
If you write already sorted output, than do:
who| sort | tee file
Because:
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.
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.
Simple & effective.
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.
very much informative..
nice work
very informative
Good presentation, thanks.
SHort and descriptive, pls continue to post. hank you