[TriLUG] `find -delete` not deleting

Joseph Mack NA3T via TriLUG trilug at trilug.org
Tue Sep 27 21:03:13 EDT 2016


On Tue, 27 Sep 2016, Ed Blackman via TriLUG wrote:

> Sorry for the late reply, but none of the other replies showed the 
> minimal change that I think will fix the problem:

thanks for your detailed explanation.

> find $top_dir -depth -mtime +7 -delete > /dev/null 2>&1
>
> find normally operates on objects as it finds them.  That's usually fine, but 
> fails when removing directory trees.  That's because -delete will not remove a 
> directory if it's not empty.  Therefore, you need to have find go all the way 
> out to the furthest leaves (files or empty directory), delete them, and then 
> move up to their parents.  -depth is the option you need to tell find to do 
> that.
>
> $ mkdir -p /tmp/test/{1,2}; touch -m -d 'now -8 days' /tmp/test/{1,2}{/{a,b},}

I'm impressed with the "{1,2}{/{a,b},}"

> $ find /tmp/test/ -mtime +7 # note that root directories come first

didn't know about this. I didn't even think of looking for it.

> /tmp/test/2
> /tmp/test/2/a
> /tmp/test/2/b
> /tmp/test/1
> /tmp/test/1/a
> /tmp/test/1/b

when I do

`find /tmp/test/ -mtime +7 -delete`

on the problem machine, I wind up with /tmp/test empty.

When I posted to the mailing list, the problem was that {1,2} had the date of 
the cronjob on them. I get the same result if I populate the directories to one 
level lower (/tmp/test/1/a/{c,d}). This result doesn't change if I do it as a 
cronjob.

I have no idea why the problem is not occuring right now.

> $ find /tmp/test/ -depth -mtime +7 # note that leaves come first
> /tmp/test/2/a
> /tmp/test/2/b
> /tmp/test/2
> /tmp/test/1/a
> /tmp/test/1/b
> /tmp/test/1
> $ find /tmp/test/ -depth -mtime +7 -delete
> $ ls /tmp/test/
> $
>
> -depth is specified in POSIX.  In my version of GNU find, the man page 
> says that -delete implicitly turns on the -depth option, but -delete 
> isn't POSIX, so I'm guessing that your find doesn't do that.

thanks for the explanations.

I don't know why I had the problem then.

Thanks
Joe


-- 
Joseph Mack NA3T EME(B,D), FM05lw North Carolina
jmack (at) austintek (dot) com - azimuthal equidistant
map generator at http://www.wm7d.net/azproj.shtml
Homepage http://www.austintek.com/ It's GNU/Linux!


More information about the TriLUG mailing list