krussell222
asked on
Cron job to delete folder/files older than n days.
I need a simple script (cron... run daily) to do some cleanup on directories older than n days, but I need to leave the top directory intact.
So basically, I need to:
1) Get a list of all directories in my current directory (this dir will always be a year.. .2007, 2008, 2009)
2) cd to that directory and delete all directories (and files) below that are older than n days.
For instance, given the following structure:
/2007
/2007/mydir/
/2008
/2008/yourdir/
I would need to delete mydir and yourdir if they were older than n days, while leaving the top level directory alone.
This is on solaris unix.
So basically, I need to:
1) Get a list of all directories in my current directory (this dir will always be a year.. .2007, 2008, 2009)
2) cd to that directory and delete all directories (and files) below that are older than n days.
For instance, given the following structure:
/2007
/2007/mydir/
/2008
/2008/yourdir/
I would need to delete mydir and yourdir if they were older than n days, while leaving the top level directory alone.
This is on solaris unix.
ASKER
The thing is, I'm not going to know which year directories are going to be in here... and I don't want to have to change the code when we role over to 2009. I'd like to be able to just kick it off and have it check each top level year directory.
ASKER CERTIFIED SOLUTION
membership
Create a free account to see this answer
Signing up is free and takes 30 seconds. No credit card required.
ASKER
This is just about it. The only thing missing is that I want the find/remove if the loop to only go one directory deep. Unfortunately, Solaris doesn't have a maxdepth arg for find. Maybe using prune or someting.
cd /2007
find . -mtime +n -exec rm -rf {} \;