[CLUE-Tech] bash and spaces

David Anselmi anselmi at anselmi.us
Tue Nov 9 16:06:56 MST 2004


Greg Knaddison wrote:
> Sorry for being dense.  Can you describe why xargs is better than
> exec?  I did a little bit of searching but couldn't find anything.

The main reason is that -exec runs your command in a separate shell for 
every file found.  So instead of getting cat run once with several 
arguments it is run several times with one argument each time.  The 
former is more efficient (not that that's a big deal and in this case 
find is probably unnecessary overhead too).

You can't do:

cat $(find...)

because you risk the command subst being longer than the allowed command 
line length (assuming there still is a limit).  (And cat $(find...) is 
backwards from the way you think about the algorithm.)

And, as I said, -print0 with xargs allows you to use a null as a word 
separator and skip all the headaches of spaces in path names.

Finally, suppose that instead of cat we were grepping.  Then:

-exec grep foo {} \;

only prints out matching lines, not the files they came from (because 
grep is only run with one arg) so you have to do something ugly like:

-exec grep foo {} /dev/null \;

so there are two args.

HTH,
Dave




More information about the clue-tech mailing list