Take the 2-minute tour ×
Server Fault is a question and answer site for system and network administrators. It's 100% free, no registration required.

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

share|improve this question

2 Answers 2

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.
share|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.

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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