Re: [SLUG] Best (most efficient method) recursive dir DEL

2014-05-22 Thread Amos Shapira
Locate only indexes path names, not other attributes (like type, size, time
etc)
On 23 May 2014 06:11, "Kyle"  wrote:

> Thanks to all for the responses.
>
> Interestingly, everyone has come back with "find" (followed by..) as
> the best option. Perhaps this is simply a reflection of the fact my 3
> examples all used 'find'.
>
> I have always thought (believed) 'find' was a less efficient process than
> 'locate' and kind of hoped 'locate' (or some other cmd I don't know) might
> pop up as a solution. I understand 'locate' depends on an updated 'db', but
> I figured that indexing process was still more efficient than 'find'
> trawling the structure in realtime.
>
> Kyle
>
>
> On 22-05-2014 19:16, Darragh Bailey wrote:
>
>> Hi Kyle,
>>
>> You might find it worth looking at the following invocation of find:
>>
>> find  -name  -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.
>>
>>  --
> SLUG - Sydney Linux User's Group Mailing List - http://slug.org.au/
> Subscription info and FAQs: http://slug.org.au/faq/mailinglists.html
>
-- 
SLUG - Sydney Linux User's Group Mailing List - http://slug.org.au/
Subscription info and FAQs: http://slug.org.au/faq/mailinglists.html


Re: [SLUG] Best (most efficient method) recursive dir DEL

2014-05-22 Thread Kyle

Thanks to all for the responses.

Interestingly, everyone has come back with "find" (followed by..) as 
the best option. Perhaps this is simply a reflection of the fact my 3 
examples all used 'find'.


I have always thought (believed) 'find' was a less efficient process 
than 'locate' and kind of hoped 'locate' (or some other cmd I don't 
know) might pop up as a solution. I understand 'locate' depends on an 
updated 'db', but I figured that indexing process was still more 
efficient than 'find' trawling the structure in realtime.


Kyle


On 22-05-2014 19:16, Darragh Bailey wrote:

Hi Kyle,

You might find it worth looking at the following invocation of find:

find  -name  -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.



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


Re: [SLUG] Best (most efficient method) recursive dir DEL

2014-05-22 Thread Amos Shapira
On 22 May 2014 19:16, Darragh Bailey  wrote:

> Hi Kyle,
>
> You might find it worth looking at the following invocation of find:
>
> find  -name  -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  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  -name  -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  -type d -name ` - does
> it
> > actually require the ' ` ' or are ' ' ' good enough?
> >
> > find  -name '' -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]

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


Re: [SLUG] Best (most efficient method) recursive dir DEL

2014-05-22 Thread Darragh Bailey
Hi Kyle,

You might find it worth looking at the following invocation of find:

find  -name  -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.


On 22 May 2014 00:10, Kyle  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  -name  -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  -type d -name ` - does it
> actually require the ' ` ' or are ' ' ' good enough?
>
> find  -name '' -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


Re: [SLUG] Best (most efficient method) recursive dir DEL

2014-05-22 Thread James Polley


> On 22 May 2014, at 9:10, Kyle  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  -name  -exec rm -rf {} \;  - what's 
> the '\' for and is it necessary?
> 
> rm -rf `find  -type d -name ` - does it 
> actually require the ' ` ' or are ' ' ' good enough?
> 
> find  -name '' -type d -delete- or won't 
> this work for a non-empty dir?

How do you define "most efficient"? Run time? CPU cycles? Memory usage? Forks? 
Disk reads/writes? Readability/maintainability?

My personal guess is that a find command that locates the things you want to 
delete and ends with "-print0 | xargs -0 rm -rf" will satisfy most of those 
criteria.

(Xargs will stuff as many file names as it thinks will fit on the command line, 
but sometimes it gets ambitious - you might have to use "xargs -0 -n 100 rm 
-rf" to limit it to 100 file names per invocation of rm)

> 
> Or is there a more efficient manner which I can slot into a cron job?
> 
> Much appreciate the input.
> 
> -- 
> 
> Kind Regards
> 
> Kyle
> 
> -- 
> SLUG - Sydney Linux User's Group Mailing List - http://slug.org.au/
> Subscription info and FAQs: http://slug.org.au/faq/mailinglists.html
-- 
SLUG - Sydney Linux User's Group Mailing List - http://slug.org.au/
Subscription info and FAQs: http://slug.org.au/faq/mailinglists.html


Re: [SLUG] Best (most efficient method) recursive dir DEL

2014-05-21 Thread Kyle

Sorry, poorly worded.

I want to to loop a given structure and remove . /[specific, named] 
/. (non-empty) directories below the top dir


Kyle


On 22-05-2014 14:12, Amos Shapira wrote:
What's the context of this question? Do you really want to keep all 
empty directories?


"-delete" will fail on non-empty directories. Use "-print0 -prune | 
xargs -0 rm -rf" to stop find from scanning the doomed directory.


On 22 May 2014 09:10, "Kyle" mailto:k...@attitia.com>> 
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  -name  -exec rm -rf {} \;
 - what's the '\' for and is it necessary?


rm -rf `find  -type d -name ` -
does it actually require the ' ` ' or are ' ' ' good enough?

find  -name '' -type d -delete-
or won't this work for a non-empty dir?



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


Re: [SLUG] Best (most efficient method) recursive dir DEL

2014-05-21 Thread Amos Shapira
(Sorry I'm writing from the phone and can't test exact solution)

What's the context of this question? Do you really want to keep all empty
directories?

"-delete" will fail on non-empty directories. Use "-print0 -prune | xargs
-0 rm -rf" to stop find from scanning the doomed directory.
On 22 May 2014 09:10, "Kyle"  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  -name  -exec rm -rf {} \;  -
> what's the '\' for and is it necessary?
>
> rm -rf `find  -type d -name ` - does it
> actually require the ' ` ' or are ' ' ' good enough?
>
> find  -name '' -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?
>
> Much appreciate the input.
>
> --
> 
> Kind Regards
>
> Kyle
>
> --
> SLUG - Sydney Linux User's Group Mailing List - http://slug.org.au/
> Subscription info and FAQs: http://slug.org.au/faq/mailinglists.html
>
-- 
SLUG - Sydney Linux User's Group Mailing List - http://slug.org.au/
Subscription info and FAQs: http://slug.org.au/faq/mailinglists.html


Re: [SLUG] Best (most efficient method) recursive dir DEL

2014-05-21 Thread Zenaan Harkness
On 5/22/14, Kyle  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  -name  -exec rm -rf {} \;  -
> what's the '\' for and is it necessary?
>
> rm -rf `find  -type d -name ` - does
> it actually require the ' ` ' or are ' ' ' good enough?
>
> find  -name '' -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  -name '' -type d | xargs rm

and if you might have spaces in dir (of file) names:
find  -name '' -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  -name '' -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


[SLUG] Best (most efficient method) recursive dir DEL

2014-05-21 Thread Kyle

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  -name  -exec rm -rf {} \;  - 
what's the '\' for and is it necessary?


rm -rf `find  -type d -name ` - does 
it actually require the ' ` ' or are ' ' ' good enough?


find  -name '' -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?

Much appreciate the input.

--

Kind Regards

Kyle

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