SUMMARY: deleting old files but not directories with new files in them [weird find results.]

From: Christopher Barnard <cbarnar1_at_earthlink.net>
Date: Tue Mar 02 2010 - 22:33:49 EST
As several folks realized and pointed out, yes I had two questions with
find in the same script.  I separated them into two posts so that they
could have separate summaries.

I asked

> I am not sure if this is a bug in the sun-shipped find (Solaris 10) or not.
>
> If I delete the files over a certain age, it deletes subdirectories as well
if
> they are over the age limit.  The problem is that the files in that
> subdirectory can be under the age limit but still get deleted.
>
> ie., I find all files under /export/staff over 365 days old and delete them
> with find.   The /export/staff/projects/stew/ directory contains files
written
> yesterday should not be deleted.  However the directory
/export/staff/projects
> is over 365 days old.  So find is dutifly doing a recursive delete on the
> projects/ subdirectory even though some of the directories under it have
> recent files.  This appears only for a problem with 2nd generation or more
of
> depth, btw ( a/ and a/b/ are ok but a/b/c/, a/b/c/d, etc are not).  So I
want
> to delete a directory only if every file and directory under it for an
> indefinite depth is over the age limit.  I thought that was the way find
> worked, but evidently not...
>
> Thoughts?  Is what I am describing doable?

Solution

no, its not a bug.  I'm trying to do too much at once.  Do it in two passes.
First, using -type f, remove elderly files.  Then do a second pass to remove
directories that are empty.

The code snippet

/usr/bin/find ${SOURCEDIR} ${ADD} -type f -mtime +${MAXAGE} -exec rm {} \;
###
### now go through ${SOURCEDIR} and look for directories over the specified
### age and are empty.  They can be deleted.  An rmdir will fail if there is
### anything in the directory.  STDERR is directed to /dev/null to suppress
### the expected errors when a directory is not empty.

/usr/bin/find ${SOURCEDIR} ${ADD} -type d -mtime +${MAXAGE} -exec rmdir {}
2>/dev/null \;

almost all of the suggestions centered around using -type and making two
passes.

Thanks to the 20-odd and counting folks who pointed out the wonders of the
-type flag and two passes...

Christopher L. Barnard
-------------------
comment your code as if the maintainer is a homicidal maniac who knows where
you live.
_______________________________________________
sunmanagers mailing list
sunmanagers@sunmanagers.org
http://www.sunmanagers.org/mailman/listinfo/sunmanagers
Received on Tue Mar 2 22:33:46 2010

This archive was generated by hypermail 2.1.8 : Thu Mar 03 2016 - 06:44:15 EST