I have a script to clean out old files and directories in a log area like:

find . -mtime +${NUM_DAYS} -type d -exec rm -rf '{}' \;

However if I run this 2 levels up from the files that have changed it will often show the parent directory because the time stamp isn't changed by changes in a sub-directory

Is there a way I can use find (or something else) so I don't try and delete the parent directory?

For example if I have the file system:

# ls -ld /var
drwxr-xr-x 24 root root 4096 Aug 16  2010 /var
# ls -ld /var/net-snmp/
drwx------ 2 root root 4096 Aug 28 15:49 /var/net-snmp/
# ls -l /var/net-snmp/
total 4
-rw------- 1 root root 1174 Aug 28 15:49 snmpd.conf

Running my find command from / with will try to recursively remove /var, even if NUM_DAYS is set so that Aug 28 is within my time scale


You could use the -mindepth option :

    -mindepth levels
        Do  not  apply  any tests or actions at levels less than levels
        (a non-negative integer).  -mindepth 1 means process all files
        except the command line arguments.
| improve this answer | |
  • Helpful as for this example it would work but I was hoping for a more general solution that would not depend on the particular directory layout. – danio Nov 2 '11 at 14:52

Assuming linux, I'd give tmpwatch a try.

| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.