Solved

Need to schedule Linux script to remove directories and their contents

Posted on 2013-05-15
4
306 Views
Last Modified: 2013-05-16
Hi,

I am looking for a script that I can schedule on a SuSE Linux Enterprise server that will run on a daily basis at 1am.  I need it to look in a certain directory for directories with folder named with the date of 90 days old or older and delete them along with their contents.

This is for a Squid box.  Every month or so, I go and delete daily logs that are older than 3 months.  Im looking to automate this.  Right now, I ssh into the box, and run the following command to remove all directories from the Month of February.

rm -rf /proxy/www/Daily/2013Feb*



If this was vbscript, it would take me a few minutes, but im not so good with linux scripting.  

If someone could also tell me how exactly to schedule the job, that would be great.
0
Comment
Question by:TakedaT
  • 2
  • 2
4 Comments
 
LVL 77

Accepted Solution

by:
arnold earned 500 total points
ID: 39168884
Dealing with logs should be done through logrotate /etc/logrotate.d see a list of files and here you can rotate the logs at a specific interval, or based on file size.  The number of copies to keep is also a setting as well as whether the files should be compressed.

Usng cron
0 1 * * * /bin/find /proxy/www/Daily -type d -mtime +90 -exec rm -rf {}\;

As a test run the find without the exec.  What it will do is look for directories that are older than 90 days since last modification and delete them and their content (-exec rm -rf {}\;)
0
 
LVL 10

Author Comment

by:TakedaT
ID: 39171276
Thanks Arnold.  The command looks like it will work perfectly without the -exec part.  But I am having some trouble adding it to cron.  I logged in as root and typed 'vi crontab'.  Then I pasted your code in there and it never ran.  I have never used cron before, can you give me some more detail how to add it?
0
 
LVL 77

Assisted Solution

by:arnold
arnold earned 500 total points
ID: 39171727
You never edit cron directly,

Use export VISUAL=vi
export EDITOR=vi
crontab -e

This should do it.
0
 
LVL 10

Author Closing Comment

by:TakedaT
ID: 39171869
OK, got it.  I actually googled and found to use 'crontab -e' but the command swill wouldn't run.  Adding a space between the last } and the \ seems to have done the trick.

Thanks
0

Featured Post

U.S. Department of Agriculture and Acronis Access

With the new era of mobile computing, smartphones and tablets, wireless communications and cloud services, the USDA sought to take advantage of a mobilized workforce and the blurring lines between personal and corporate computing resources.

Question has a verified solution.

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

How many times have you wanted to quickly do the same thing to a list but found yourself typing it again and again? I first figured out a small time saver with the up arrow to recall the last command but that can only get you so far if you have a bi…
Linux users are sometimes dumbfounded by the severe lack of documentation on a topic. Sometimes, the documentation is copious, but other times, you end up with some obscure "it varies depending on your distribution" over and over when searching for …
Learn several ways to interact with files and get file information from the bash shell. ls lists the contents of a directory: Using the -a flag displays hidden files: Using the -l flag formats the output in a long list: The file command gives us mor…
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.:

920 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

16 Experts available now in Live!

Get 1:1 Help Now