home | O'Reilly's CD bookshelfs | FreeBSD | Linux | Cisco | Cisco Exam  


Unix Power ToolsUnix Power ToolsSearch this book

43.8. Send Output Two or More Places

Figure Go to http://examples.oreilly.com/upt3 for more information on: tee

If you're running a program and you want to send its output to a file -- but you want to see the output on your screen, too, so you can stop the program if something goes wrong -- you can use tee. The tee program reads its standard input and writes it to one or more files. (The web site has the GNU version.)

NOTE: A pipe may buffer the output of a program, collecting it in chunks and spitting it out every so often. If the program's output comes slowly and feeds tee through a pipe, there might be long delays before you see any output. In that case, it's better to use > to redirect output to a file, put the program into the background, and watch the output with tail -f (Section 12.10). Or use a program like script (Section 37.7).

Use tee for saving results in the middle of a long pipeline of commands. That's especially good for debugging. For example, you could type:

% prog | tee prog.out | sed -f sedscr | tee sed.out | ...

to save the output of prog in the file prog.out and also pipe it to the sed command, save sed's output in sed.out and also pipe it, and so on.

Here are two other notes about tee. If you want to add to a file that already exists, use the -a option. tee can write to more than one file if you give all of the filenames as arguments

Z shell users usually don't need tee because they have the zsh MULTIOS option. For instance, here's how to write the pipeline above:

zsh% setopt multios
zsh% prog > prog.out | sed -f sedscr > sed.out | ...

-- JP



Library Navigation Links

Copyright © 2003 O'Reilly & Associates. All rights reserved.