Take the 2-minute tour ×
Super User is a question and answer site for computer enthusiasts and power users. It's 100% free, no registration required.

How can I pipe the output of one command to the input of two other commands simultaneously?

share|improve this question
4  
Yes. See this stackoverflow question: stackoverflow.com/questions/670784/… –  Blair Conrad Jul 17 '09 at 20:10
add comment

2 Answers

up vote 43 down vote accepted

It sounds like the tee command will do what you want.

The key is to use

>( )

for process substitution. With tee, use the following pattern:

tee >(proc1) >(proc2) >(proc3) | proc4

So if you wanted to use the output of ls as input to two different grep programs, save the output of each grep to different files, and pipe all of the results through less, try:

ls -A | tee >(grep ^[.] > hidden-files) >(grep -v ^[.] > normal-files) | less

The results of the ls -A will "piped" into both greps. The file hidden-files will have the contents from the output of the first grep, and normal-files will have the results of the second grep. All of the files will be shown in the pager less.

source

share|improve this answer
3  
this one is very good ! –  hayalci Jul 17 '09 at 20:33
2  
+1 because even after 10 years of shell scripting, I'd never seen this! –  jtimberman Jul 31 '09 at 5:42
add comment

Use "tee".

Example:

grep someSearchString someFile | tee /dev/tty | wc -l > grepresult

This will send the output of the grep command to both the terminal and to wc (whose output is in turn redirected to the file grepresult).

"Tee" is explained in the Wikipedia article tee (command). Central is: "The tee command reads standard input, then writes its content to standard output and simultaneously copies it into the specified file(s) or variables.".

share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.