Softpanorama
(slightly skeptical) Open Source Software Educational Society

May the source be with you, but remember the KISS principle ;-)

Google   


Unix tee command

News Shells Recommended Links Options Examples      
Pipes Pipes in Loops Shell Input and Output Redirection Process Substitution in Shell script AWK xargs  
  find grep sort cut uniq Exit Status Etc

Tee is a very interesting Unix command. It is quintessential Unix command as it deals with pipes. The idea is to split pipe so that you write to the standard output (most commonly the next stage of the pipe) and to a file simultaneously.

Tee is also very useful in complex pipes debugging: you place the tee command anywhere in a pipe command to divert a copy of the standard input (of tee) to disk and analyze correctness of this stage.  A special variant of the tee for the shell is called script and permits duplicating all input commands submitted to a shell into a file.

The name is also very apt: while it is taken from the world of plumbing it perfectly describes the purpose.

Another important usage is to split the stream on several substreams. This way very complex pipes can be implemented in Unix.  Target in tee can be named pipe

A typical  example of tee usage would be

sort somefile.txt | tee sorted_file.txt |  uniq -c |  head 12 > top12.txt

More complex example would be  processing records of proxy usage that contain code 403 to extract champions, top 12 users who got the most of rejections:

# gzip -d -c all_code403.gz | cut -d ' ' -f 1 | sort -k 1 | uniq -c | sort -r \
| tee ./d$1/all_users | head -12 > ./d$1/all_champs 

here we first write all the users sorted in reverse order to the disk and then generated the top 12 using head.  Un this case standard output of tee is piped to head

You can use the tee command to debug complex pipes that for some reason do not work as expected.  It can be placed anywhere in a pipe to check what the output looks like at a certain point.  you can also save some pipes stages output for future usage if it represents the result of the stage that takes a lot of time. Such situation often arise of you process proxy or HTTP server logs using pies.

The other interesting usage of tee is to split pipe into several substreams using named pipes, for example

mkfifo pipe1 pipe2  # create named pipes
echo ***
gzip -d -c all$1.gz | tee pipe1 | grep '" 200' | tee pipe2 >(gzip > all$1ok.gz) | cut -d '"' -f 2 | cut -d '/' -f 3 | tr '[:upper:]' '[:lower:]' | sort | uniq -c | sort -r > ./d$1/oksites$1 & \
<pipe1 cut -d ' ' -f 1 | grep '" 403 ' | sort | uniq -c | sort -r > ./d$1/all$1xuser & \
<pipe2 cut -d ' ' -f 1 | sort | uniq -c | sort -r | tee ./d$1/all$1users | head -12 > ./d$1/all$1champ

Syntax

Following is the general format of the tee command.

     tee [ -ai ] file_list

Options

The following options may be used to control how tee functions.

-a Appends the output to an existing file. If the file does not exist it will be created. Normally an existing file would be overwritten.
-i Ignore interrupts. If you press the Delete key, tee ignores the interrupt signal sent to it and continues processing.

Arguments

The following argument may be passed to the tee command.

file_list One or more files where tee writes copies of the input.

 

Examples

Type nawk -F: '{ print $1}' /etc/passwd | sort | tee users | lp and press Return. Notice the message returned by lp. You now have a printout on the default printer. The output is also stored in the file named users.

Old News ;-)

TTTT tee

Sometimes you want to be able to monitor the progress of a long running process, but you also want to save a transcript of its output so you don't have to watch it every second.

tee was designed with this very purpose in mind.

 


% ls -l | tee foobar
total 26
-rw-r--r--  1 jeffy          28 May  9 16:12 Makefile
-rwxr-xr-x  1 jeffy       24576 May 28 11:31 foo
-rw-r--r--  1 jeffy          57 May  9 16:13 foo.c
% ls
Makefile        foo             foo.c           foobar
% cat foobar
total 26
-rw-r--r--  1 jeffy          28 May  9 16:12 Makefile
-rwxr-xr-x  1 jeffy       24576 May 28 11:31 foo
-rw-r--r--  1 jeffy          57 May  9 16:13 foo.c

Keep in mind that tee only duplicates stdout, so if you want to save error output (you probably do), you'll need to combine stderr onto stdout with "|&" if you're in csh-land, or "2>&1" if you're playing with sh.

Also note that tee accepts a "-a" flag which tells it to append its output to the file instead of truncating the file first.

Why's it called tee, you ask? Named after the pipe fitting that looks like a letter "T" and sends its input to two different places.

tee is also distinguished by having one of the shorter man pages in UNIXdom.


% cd /vobs/App/Mr
% clearmake |& tee Transcript

Recommended Links

Tee (Unix) - Wikipedia, the free encyclopedia

[IBM AIX] Chapter 4. Input and Output Redirection

Ad Hoc Data Analysis From The Unix Command Line - Wikibooks, collection of open-content textbooks

Speaking UNIX, Part 1 Command the power of the command line

Command Pipelines

 


Copyright © 1996-2007 by Dr. Nikolai Bezroukov. www.softpanorama.org was created as a service to the UN Sustainable Development Networking Programme (SDNP) in the author free time. Submit comments This document is an industrial compilation designed and created exclusively for educational use and is placed under the copyright of the Open Content License(OPL). Original materials copyright belong to respective owners. Quotes are made for educational purposes only in compliance with the fair use doctrine.

Standard disclaimer: The statements, views and opinions presented on this web page are those of the author and are not endorsed by, nor do they necessarily reflect, the opinions of the author present and former employers, SDNP or any other organization the author may be associated with. We do not warrant the correctness of the information provided or its fitness for any purpose.

Last modified: March 15, 2008