Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

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

Posted on 2008-06-11
4
Medium Priority
?
1,534 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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 2000 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

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Every server (virtual or physical) needs a console: and the console can be provided through hardware directly connected, software for remote connections, local connections, through a KVM, etc. This document explains the different types of consol…
Ready to get certified? Check out some courses that help you prepare for third-party exams.
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 shows how to set up a shell script to accept a positional parameter when called, pass that to a SQL script, accept the output from the statement back and then manipulate it in the Shell.
Suggested Courses

604 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