[svlug] Re: [svlug]how to copy a bunch of "." files?

Joey Hess joey at kitenet.net
Sat May 6 16:57:33 PDT 2000

dfox at belvdere.vip.best.com wrote:
> But based on what you said, I'd expect to see a forest of grep
> processes running, one per source file in the linux source tree, all
> looking for something (for the test, I looked in all source files for
> the word 'panic'). I did not notice anything forking new processes,
> despite repeated utterances of 'ps alx'. But it took substantially
> longer, with more think time at the start of the process than with
> an xargs solution.

No, you misunderstood. He said it forked off a grep for _each_request_,
not a ton of parallel greps. 

find . -exec grep foo {} \;

If the directory contains files named 1 through 10, this causes the
following programs to run, in sequence:

grep foo 1
grep foo 2
grep foo 3
grep foo 4
grep foo 5
grep foo 6
grep foo 7
grep foo 8
grep foo 9
grep foo 10

Whereas this:

find . | xargs grep foo

Causes this single program to be run:

grep foo 1 2 3 4 5 6 7 8 9 10

You decide which is going to be faster. Calling grep 10 times, or once.

> Furthermore, the xargs gives the data in a better presentation: the
> find -exec doesn't give the filenames, but the xargs does (using
> find . -name "*.[ch] | xargs grep 'panic' -).

You can't count on that, xargs can only pass so many arguments to a
command at once, so if you were grepping 1001 files and it decided to
only pass in 1000 arguments at a time, it would run:

grep foo 1 2 ... 999 1000
grep foo 1001

Any matches found by the second grep would not be prefixed by the
filename. If you want to ensure filename prefixing, just use grep

#!/usr/bin/perl -li361M61AAM61AGMAM61AG261_G326G026M03_MG
;y!MAG!\\ /!;print' 'x$x--,$_}# MC Escher meets Joey Hess

More information about the svlug mailing list