Super User is a question and answer site for computer enthusiasts and power users. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

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

share|improve this question
Yes. See this stackoverflow question:… – Blair Conrad Jul 17 '09 at 20:10
up vote 50 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 be "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. EDIT: what you see in less is the same exact output of ls -A, not the result of the greps. If you want to modify the output from ls -A to less, (e.g. swapping the order so normal files are listed before hidden ones) then try this:

ls -A | tee >(grep ^[.]) >(grep -v ^[.]) >/dev/null | less

Without >/dev/null, the output of greps would be appended to the output of ls -A instead of replacing it.


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

Use "tee".


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

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .