[darcs-users] Re: a bash tip for easily excluding the _darcs directory

Ralph Corderoy ralph at inputplus.co.uk
Wed Mar 9 18:15:57 UTC 2005


Hi Steven,

> > Please consider suggesting xargs(1) since it saves many invocations
> > of grep.
> 
> Why is grep invoked fewer times with xargs rather than find's "-exec"
> clause?

It's its raison d'etre.  find(1) executes the -exec commands once each
time it reaches an -exec point in evaluating the expression you've
provided.  If the program you wish to execute can cope with many files
being given as arguments in one invocation, e.g. rm(1), then there's a
lot of overhead in N x fork(2) and execve(2) for N files using -exec
compared with one fork and exec with argv[] containing all the files.

xargs(1) knows about the limits on the number of command-line arguments,
and about the limit on the total number of characters in the command
line, and reads lines from stdin, appending them to the command you
provide, until stdin's empty or the command line has run out of space.
It executes the command and keeps reading.  Less forks and execs can be
a big win.

    $ find ?       
    a
    b
    c
    $ find ? -exec echo {} + {} \;
    a + a
    b + b
    c + c
    $ find ? | xargs echo
    a b c
    $ 

xargs is better than

    echo `find ?`

since this only runs one echo and the shell will error if find's output
is too large to fit for it to construct an argv for echo.

Usenet's comp.unix.shell is probably a good place to find out more.
There's bound to be a FAQ too.

Cheers,


Ralph.





More information about the darcs-users mailing list