Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

Need to schedule Linux script to remove directories and their contents

Posted on 2013-05-15
4
Medium Priority
?
315 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 80

Accepted Solution

by:
arnold earned 2000 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 80

Assisted Solution

by:arnold
arnold earned 2000 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

Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

Question has a verified solution.

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

Setting up Secure Ubuntu server on VMware 1.      Insert the Ubuntu Server distribution CD or attach the ISO of the CD which is in the “Datastore”. Note that it is important to install the x64 edition on servers, not the X86 editions. 2.      Power on th…
Google Drive is extremely cheap offsite storage, and it's even possible to get extra storage for free for two years.  You can use the free account 15GB, and if you have an Android device..when you install Google Drive for the first time it will give…
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…
Connecting to an Amazon Linux EC2 Instance from Windows Using PuTTY.
Suggested Courses
Course of the Month12 days, 1 hour left to enroll

916 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