Tee Command Usage Examples

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.

  • loki March 13, 2010, 7:44 am

    a very good book , like it very much

  • asha July 29, 2011, 5:28 am

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

  • nikesh August 30, 2011, 8:20 am

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

  • sindhu September 2, 2011, 7:12 am

    excellent work and understandable to everyone.

  • Ambar Agrawal December 1, 2011, 2:33 am

    Kudos to author!!

    Excellent!!!

  • abhijit jena January 18, 2012, 11:22 pm

    can you help me about the autosys

  • karin May 7, 2012, 8:28 am

    It doesn’t work!!!!

  • Neil Anghan July 29, 2012, 11:48 pm

    Excellent

  • Sumit Chauhan September 3, 2012, 11:01 pm

    Your posts are really helpful.

  • simil September 7, 2012, 6:45 pm

    nice explanation

  • bala November 19, 2012, 7:22 pm

    excellent explanation.

  • ajay June 27, 2013, 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

  • Pradip Sabhadiya July 2, 2013, 7:36 pm

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

  • gurdeep August 8, 2013, 8:42 am

    nice one… Excellent

  • Shakthi September 11, 2013, 9:30 pm

    Thank you!
    It helped.

  • harsha September 19, 2013, 3:08 am

    nice.

  • yamalaiah September 24, 2013, 8:49 pm

    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)

  • artickl September 27, 2013, 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

  • artickl September 27, 2013, 9:36 pm

    @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.

  • V September 28, 2013, 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.

  • rohit uppal October 8, 2013, 10:07 pm

    Simple & effective.

  • Sampath Kumar January 15, 2014, 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.

  • Divya January 23, 2014, 10:14 pm

    very much informative..

  • Manoj J January 28, 2014, 12:04 pm

    nice work

  • vivek February 7, 2014, 5:12 am

    very informative

  • Bernie March 21, 2014, 11:10 am

    Good presentation, thanks.

  • Iseyas April 7, 2014, 6:43 pm

    SHort and descriptive, pls continue to post. hank you