On 5/22/14, Kyle <[email protected]> wrote:
> I was wondering what is the best (as in most efficient method) for doing
> an automated, scheduled recursive search and DEL exercise. The scheduled
> part is just a cron job, no problem. But what's the most efficient
> method to loop a given structure and remove all (non-empty) directories
> below the top dir?
>
> The 3 examples I've come up with are;
>
> find <top_dir> -name <name_to_find_and_DEL> -exec rm -rf {} \;      -
> what's the '\' for and is it necessary?
>
> rm -rf `find <top_dir> -type d -name <name_to_find_and_DEL>`     - does
> it actually require the ' ` ' or are ' ' ' good enough?
>
> find <top_dir> -name '<name_to_find_and_DEL>' -type d -delete    - or
> won't this work for a non-empty dir?

What you want is to batch the commands ultimately doing the work, into
as few batches as possible, to minimize process startup and teardown
overhead (assuming this is large - if you're only dealing with a few
10s of directories, I wouldn't worry about it - but if you're deleting
1000s then it _may_ be worth improving upon). In either case, for
batching you want "xargs".

So eg:
find <top_dir> -name '<name_to_find_and_DEL>' -type d | xargs rm

and if you might have spaces in dir (of file) names:
find <top_dir> -name '<name_to_find_and_DEL>' -type d -print0 | xargs -0 rm

Sometimes, in some environments (??) xargs creates command lines which
are too long, so sometimes I limit the batch size like so:
find <top_dir> -name '<name_to_find_and_DEL>' -type d -print0 | xargs
-0 -n200 rm

(each of my 3 examples are a single line, email will likely wrap)

I do not know about your third command, whether find does batching or
process forking in this regard, and I can't be bothered to bring up
the man page for find's -delete (you can do that, and let us know your
conclusions).

Your second example is sort of like xargs, but xargs has some niceties
like N-sized batching and handling files with spaces.

Good luck,
Zenaan
-- 
SLUG - Sydney Linux User's Group Mailing List - http://slug.org.au/
Subscription info and FAQs: http://slug.org.au/faq/mailinglists.html

Reply via email to