Wednesday, November 30, 2005

Pipes

You see, wire telegraph is a kind of a very, very long cat. You pull his tail in New York and his head is meowing in Los Angeles. Do you understand this? And radio operates exactly the same way: you send signals here, they receive them there. The only difference is that there is no cat. - Albert Einstein
The shell's biggest innovation is pipes.

To hook stdout of one program to stdin of another, you just connect the two programs with a pipe symbol: '|'.

How many files do I have?
ls | wc -l
You can think of it as shorthand for this:
ls > TMPFILE; wc -l TMPFILE; rm TMPFILE
The only thing is, there is no temporary file. In a pipeline, the output from one command really is hooked up to the input of the next, and both processes run simultaneously. It's magic.

If the first process is too slow, and the pipe gets empty, the second process just sits and waits for the first one to send it more stuff. If the first process is too fast, and the pipe starts to clog, it just blocks until the second sucks enough out of the pipe to make it worth starting up again.

For me, the ability to pipe into wc -l, alone, is invaluable.

How many processes are running?
ls -d /proc/[0-9]* | wc -l
How many .c and .h files are there in my source tree?
find . -name '*.[ch]' | wc -l
How many files have we had to change since the "FINAL" release?
cvs -nq update -rFINAL | wc -l
Next time you're asking "How many ...?", remember to answer it with a pipe.

0 Comments:

Post a Comment

<< Home