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

{ 11 comments… read them below or add one }

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!!

Excellent!!!

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

Excellent

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.

Leave a Comment

Previous post:

Next post: