Solved

Need to schedule Linux script to remove directories and their contents

Posted on 2013-05-15
4
304 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 76

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 76

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

Maximize Your Threat Intelligence Reporting

Reporting is one of the most important and least talked about aspects of a world-class threat intelligence program. Here’s how to do it right.

Join & Write a Comment

SSH (Secure Shell) - Tips and Tricks As you all know SSH(Secure Shell) is a network protocol, which we use to access/transfer files securely between two networked devices. SSH was actually designed as a replacement for insecure protocols that sen…
It’s 2016. Password authentication should be dead — or at least close to dying. But, unfortunately, it has not traversed Quagga stage yet. Using password authentication is like laundering hotel guest linens with a washboard — it’s Passé.
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…
Get a first impression of how PRTG looks and learn how it works.   This video is a short introduction to PRTG, as an initial overview or as a quick start for new PRTG users.

746 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

12 Experts available now in Live!

Get 1:1 Help Now