Solved

Need to schedule Linux script to remove directories and their contents

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

Gigs: Get Your Project Delivered by an Expert

Select from freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely and get projects done right.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Ubuntu not booting - How get past GRUB? 3 41
Backup & Restore 3 54
winscp where are logs stored 3 34
Linux Desktop suggestion for Dell Inspiron 3043 13 29
Using 'screen' for session sharing, The Simple Edition Step 1: user starts session with command: screen Step 2: other user (logged in with same user account) connects with command: screen -x Done. Both users are connected to the same CLI sessio…
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 how to navigate the file tree with the shell. Use pwd to print the current working directory: Use ls to list a directory's contents: Use cd to change to a new directory: Use wildcards instead of typing out long directory names: Use ../ to move…
Connecting to an Amazon Linux EC2 Instance from Windows Using PuTTY.

786 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