Solved

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

Posted on 2008-06-11
4
1,528 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

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
post4 challenge 28 104
remove a combination of patterns from a file 15 70
SQUD PROXY SERVER, UNIX, SLL/HTTPS 5 103
Authenticate using sesu from script 7 56
I promised to write further about my project, and here I am.  First, I needed to setup the Primary Server.  You can read how in this article: Setup FreeBSD Server with full HDD encryption (http://www.experts-exchange.com/OS/Unix/BSD/FreeBSD/A_3660-S…
Java performance on Solaris - Managing CPUs There are various resource controls in operating system which directly/indirectly influence the performance of application. one of the most important resource controls is "CPU".   In a multithreaded…
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.
In a previous video, we went over how to export a DynamoDB table into Amazon S3.  In this video, we show how to load the export from S3 into a DynamoDB table.

860 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