Tricky Deleting

Nicholas Leippe nick at byu.edu
Tue Mar 8 17:52:20 MST 2005


On Tuesday 08 March 2005 05:35 pm, Eric Jensen wrote:

> >
> >find /2005/03/ -maxdepth 1 -type d | xargs rm -rf
> Close, tried that already but the problem is it finds /2005/03/ and
> performs rm -rf on it as well.  I am trying to play with expressions so
> I can do something like !/2005/03/ but I am new to find and not doing to
> hot on syntax.

Sorry. Use:

find /2005/03/ -maxdepth 1 -mindepth 1 -type d | xargs rm -rf

>
> Hmm... Just changed /2005/03/* and that seemed to work better.  Quick
> question, what is the difference of doing something like:
>
> `find stuff | rm -rf ` and `find stuff | xargs rm -rf `?

`find stuff | rm -rf` sends its output directly to rm's stdin, which will
do nothing, as rm ignores stdin.  (it will probably shout an error at you)

What you might have been asking is the difference between:

rm -rf `find stuff`        # and:
find stuff | xargs rm -rf

In the former, the shell replaces the contents of the back-tick expression (in 
this case `find stuff`) with the results of executing the expression, thus rm 
is passed a list of files via its command line, which is what it expects, and 
will work... until the list gets too long.  If the list exceeds the limit for 
a command line it will simply error out.

The latter overcomes this issue by passing the list of files first to xargs 
via xarg's stdin.  xargs then feeds each item in the list as a command line 
parameter to 'rm -rf' one at a time.  It is possible to have xargs pass the 
list grouped in batches--but it will make sure it never exceeds the command 
line size limit.

The former is much faster as it will invoke rm exactly once, but fails when 
the list is too big.
The latter is slower in that it will invoke rm once per file, but can handle a 
list of any length.

The performance difference is probably not significant in most cases these 
days.



-- 
Respectfully,

Nicholas Leippe
Sales Team Automation, LLC
1335 West 1650 North, Suite C
Springville, UT  84663 +1 801.853.4090
http://www.salesteamautomation.com



More information about the PLUG mailing list