Solved

Cron job to delete folder/files older than n days.

Posted on 2008-06-11
4
1,525 Views
Last Modified: 2013-12-27
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.
0
Comment
Question by:krussell222
  • 2
  • 2
4 Comments
 
LVL 40

Expert Comment

by:omarfarid
ID: 21765894
try

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


   
0
 

Author Comment

by:krussell222
ID: 21766153
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.  
0
 
LVL 40

Accepted Solution

by:
omarfarid earned 500 total points
ID: 21766187
for name in `ls`
do
if [ -d $name ]
then
      cd $name
      find . -mtime +n -exec rm -rf {} \;
      cd ..
fi
done
0
 

Author Comment

by:krussell222
ID: 21766244
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.
0

Featured Post

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

Introduction: Load and Save to file, Document-View interaction inside the SDI. Continuing from the second article about sudoku.   Open the project in visual studio. From the class view select CSudokuDoc and double click to open the header …
Introduction: Finishing the grid – keyboard support for arrow keys to manoeuvre, entering the numbers.  The PreTranslateMessage function is to be used to intercept and respond to keyboard events. Continuing from the fourth article about sudoku. …
Learn how to find files with the shell using the find and locate commands. Use locate to find a needle in a haystack.: With locate, check if the file still exists.: Use find to get the actual location of the file.:
This video will show you how to get GIT to work in Eclipse.   It will walk you through how to install the EGit plugin in eclipse and how to checkout an existing repository.

705 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

13 Experts available now in Live!

Get 1:1 Help Now