1. This site uses cookies. By continuing to use this site, you are agreeing to our use of cookies. Learn More.

Understanding & Using The tee Command

Discussion in 'Unix' started by pradeep, May 14, 2013.

  1. pradeep

    pradeep Team Leader

    Joined:
    Apr 4, 2005
    Messages:
    1,646
    Likes Received:
    86
    Trophy Points:
    0
    Occupation:
    Programmer
    Location:
    Kolkata, India
    Home Page:
    tee command is common on Unix like systems and on Windows PowerShell. The tee command writes the input to the file specified and also to the standard output, it was named after the plumbing T-splitter used. Here's a graphical presentation of the tee command:

    [​IMG]
    Source: Wikipedia.org

    Usage



    The usage can be better demonstrated than explained in theory, follow the few examples below:

    Code:
    [pradeep@castellano]$ ls -1 | tee sweetu.lst
    1311337262_info.png
    android.pl
    android_apps.html
    apps.dat
    images
    info_icon.png
    jquery.lightbox_me.js
    llsh.gif
    main.tpl.html
    ssearch.pl
    style.css
    teaser.gif
    text.php
    vikas.php
    [pradeep@castellano]$ cat sweetu.lst
    1311337262_info.png
    android.pl
    android_apps.html
    apps.dat
    images
    info_icon.png
    jquery.lightbox_me.js
    llsh.gif
    main.tpl.html
    ssearch.pl
    style.css
    teaser.gif
    text.php
    vikas.php
    
    The output of the ls command was saved to the file as well as shown on the screen.

    Example of Practical Use



    Here, I'll show some practical applications of the tee command for learning and well as implementing if required.

    Download & untar a tarball on the fly:
    Code:
    [pradeep@castellano]$  wget -O-  http://search.cpan.org/CPAN/authors/id/S/SP/SPRADEEP/Data-RoundRobinShared-0.11.tar.gz  | tee Module.tgz | tar xvz
    --2013-05-14 00:53:35--  http://search.cpan.org/CPAN/authors/id/S/SP/SPRADEEP/Data-RoundRobinShared-0.11.tar.gz
    Resolving search.cpan.org... 199.15.176.161
    Connecting to search.cpan.org|199.15.176.161|:80... connected.
    HTTP request sent, awaiting response... 302 Found
    Location: http://www.cpan.org/authors/id/S/SP/SPRADEEP/Data-RoundRobinShared-0.11.tar.gz [following]
    --2013-05-14 00:53:35--  http://www.cpan.org/authors/id/S/SP/SPRADEEP/Data-RoundRobinShared-0.11.tar.gz
    Resolving www.cpan.org... 2620:101:d000:8::140:1, 2a01:608:2:4::2, 207.171.7.177, ...
    Connecting to www.cpan.org|2620:101:d000:8::140:1|:80... connected.
    HTTP request sent, awaiting response... 200 OK
    Length: 2771 (2.7K) [application/x-gzip]
    Saving to: STDOUT
    
    100%[======================================>] 2,771       --.-K/s   in 0s
    
    2013-05-14 00:53:36 (11.8 MB/s) - written to stdout [2771/2771]
    
    Data-RoundRobinShared-0.11/
    Data-RoundRobinShared-0.11/README
    Data-RoundRobinShared-0.11/META.yml
    Data-RoundRobinShared-0.11/Changes
    Data-RoundRobinShared-0.11/t/
    Data-RoundRobinShared-0.11/t/Data-RoundRobinShared.t
    Data-RoundRobinShared-0.11/MANIFEST
    Data-RoundRobinShared-0.11/lib/
    Data-RoundRobinShared-0.11/lib/Data/
    Data-RoundRobinShared-0.11/lib/Data/RoundRobinShared.pm
    Data-RoundRobinShared-0.11/Makefile.PL
    [pradeep@castellano]$ ls -l Module.tgz
    -rw-rw-r-- 1 pradeep pg1555168 2771 May 14 00:53 Module.tgz
    
    Copy a file to multiple locations:
    Code:
    [pradeep@castellano]$ cat myfile | tee dest1 dest2 > /dev/null 2>&1
    
    Run a command on more than 1 host at the same time:
    Code:
    echo "uptime" | tee >(ssh anjali.pradeep.home) >(ssh downloader.pradeep.home) >(ssh nas.pradeep.home)
    
     
    shabbir likes this.

Share This Page