Link to home
Start Free TrialLog in
Avatar of krussell222
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.
Avatar of omarfarid
omarfarid
Flag of United Arab Emirates image

try

cd /2007
find . -mtime +n -exec rm -rf {} \;


   
Avatar of krussell222
krussell222

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
Avatar of omarfarid
omarfarid
Flag of United Arab Emirates image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
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.