* This one time, at band camp, Erik de Castro Lopo said:
> It really blew my mind when Matthew Dalton <[EMAIL PROTECTED]> said:
> 
> > #!/bin/sh
> > for file in thing.mp3 pants.txt yer.rtf others.html; do
> >     find . -name $file | xargs rm -f
> > done
> 
> Thats no good. You've got a pipe that you don't need. Try this:
> 
> 
> #!/bin/sh
> for file in thing.mp3 pants.txt yer.rtf others.html; do
>       find . -name $file -exec rm -f {} \;
> done

I disagree - but hear me out! ;-)

Although both solutions work, the first example (xargs one) will run
quicker and fork fewer processes.  Using the exec argument to find will
cause a single rm statement to be run for each file - not particularly
concerning when you are only talking about 5 files, but 5000?

Using xargs in place of exec will cause the grouping of 'rm' commands -
so that you would get 'rm thing.mp3 pants.txt yer.rtf others.html' in
one command.  The buffer for xargs is quite big and once it gets more
arguments that it can handle, it submits the command and starts over.
This results in far fewer commands being run and far faster execution of
the code.

Though this won't help in this circumstance (to any noticable level)

======================================================================
EXAMPLE

tgreen@animal:~$ du -s test/
21806   test

tgreen@animal:~$ du -s test2/
21806   test2

tgreen@animal:~$ time find test -exec rm -f {} \;
real    0m0.270s
user    0m0.120s
sys     0m0.100s

tgreen@animal:~$ time find test2 | xargs rm 
real    0m0.137s
user    0m0.020s
sys     0m0.050s
======================================================================

The amount of data and number of files was small, but it halved the
execution time on this simple test.

Happy to hear any alternatives or comments....

HTH

Greeno
-- 
Greeno <[EMAIL PROTECTED]>
GnuPG Key :  1024D/B5657C8B 
Key fingerprint = 9ED8 59CC C161 B857 462E  51E6 7DFB 465B B565 7C8B

Imagine working in a secure environment and finding the string 
_NSAKEY in the OS binaries without a good explanation
    -Alan Cox 04/05/2001

-- 
SLUG - Sydney Linux User Group Mailing List - http://slug.org.au/
More Info: http://lists.slug.org.au/listinfo/slug

Reply via email to