Solved

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

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

Problems using Powershell and Active Directory?

Managing Active Directory does not always have to be complicated.  If you are spending more time trying instead of doing, then it's time to look at something else. For nearly 20 years, AD admins around the world have used one tool for day-to-day AD management: Hyena. Discover why

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Fibonacci challenge 11 121
Write an app 10 70
mobaxterm not able to change directory 28 148
ipconfig not working on windows 10 bash shell; what other commands are missing. 17 171
Introduction: Dialogs (2) modeless dialog and a worker thread.  Handling data shared between threads.  Recursive functions. Continuing from the tenth article about sudoku.   Last article we worked with a modal dialog to help maintain informat…
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…
Learn how to get help with Linux/Unix bash shell commands. Use help to read help documents for built in bash shell commands.: Use man to interface with the online reference manuals for shell commands.: Use man to search man pages for unknown command…
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.:

770 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