Solved

Need to schedule Linux script to remove directories and their contents

Posted on 2013-05-15
4
312 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
  • 2
4 Comments
 
LVL 79

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 79

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

Tutorials alone can't teach real engineering

So we built better training tools.

-Hands-on Labs
-Instructor Mentoring
-Scenario-Based Tests
-Dedicated Cloud Servers

All at your fingertips. What are you waiting for?

Question has a verified solution.

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

Little introduction about CP: CP is a command on linux that use to copy files and folder from one location to another location. Example usage of CP as follow: cp /myfoder /pathto/destination/folder/ cp abc.tar.gz /pathto/destination/folder/ab…
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…
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.
Suggested Courses

623 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