On 22 May 2014 19:16, Darragh Bailey <[email protected]> wrote:
> Hi Kyle,
>
> You might find it worth looking at the following invocation of find:
>
> find <top_dir> -name <name_to_del> -exec rm -rf {} \+ -prune
>
> the '+' will support expansion of arguments, thus it works exactly like
> xargs in building up a command line that is passed to rm. You may also need
> to specify \"{}\" to handle whitespace in directory names, untested.
>
Kudos for bringing this up. I wasn't aware of the "+" option.
1. There is no need to quote the {}, find will pass the file names already
as separate arguments without splitting them on white space.
2. As you demo above but possibly worth to stress - the "+" form does NOT
take a terminating ";".
Test (my Mac home directory, which contains a few standard directory names
with spaces):
~$ gfind -maxdepth 1 -type d -exec ls -dF {} \+
./ ./Downloads/ ./Sites/
./.Trash/ ./Google Drive/ ./Snapshots/
./.config/ ./Library/ ./VirtualBox VMs/
./.ssh/ ./Movies/ ./bin/
./.vagrant.d/ ./Music/ ./git-dotfiles/
./Applications/ ./Pictures/ ./macports/
./Desktop/ ./Programs/ ./tmp/
./Documents/ ./Public/
Notice how "ls" is passed the right directory names for "Google Drive" and
"VirtualBox VMs"
--Amos
>
> On 22 May 2014 00:10, Kyle <[email protected]> wrote:
>
> > Hi folks,
> >
> > 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?
> >
>
> You need to escape ';' from the shell, otherwise it will think it's the end
> of the command and strip it from what is passed to 'find' which will in
> turn exit with an exception in that it couldn't work out where the end of
> the 'exec' command occurred.
>
>
>
> >
> > 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?
> >
> > Or is there a more efficient manner which I can slot into a cron job?
> >
>
>
> As someone else already pointed out, it'll probably depend on
>
> --
> Darragh Bailey
> "Nothing is foolproof to a sufficiently talented fool"
> --
> SLUG - Sydney Linux User's Group Mailing List - http://slug.org.au/
> Subscription info and FAQs: http://slug.org.au/faq/mailinglists.html
>
--
[image: View my profile on LinkedIn]
<http://www.linkedin.com/in/gliderflyer>
--
SLUG - Sydney Linux User's Group Mailing List - http://slug.org.au/
Subscription info and FAQs: http://slug.org.au/faq/mailinglists.html