[ale] bash script

David A. De Graaf dad at datix.us
Tue Sep 11 11:14:42 EDT 2007


On Tue, Sep 11, 2007 at 10:18:17AM -0400, Marvin, International Martian of Mystery wrote:
> I'd like some feedback/critique for a script I'm running as a cron job
> daily on a box I administrate.  It's supposed to delete all the files it
> finds that are 5 days or older.  
> 
> 
> #! /bin/sh
> 
> ## Remove ZoneMinder events cache
> ## There's gotta be a better way  to
> ## do this!
> 
> EXECUTE="/usr/bin/find ./ ! -name "*.log" -mtime 5  -print | /sbin/rm
> -Rf $1"
> 
> 
> cd /var/www/zm/events/1
>         $EXECUTE
> cd /var/www/zm/events/2
>         $EXECUTE
> cd /var/www/zm/events/3
>         $EXECUTE
> cd /var/www/zm/events/5
>         $EXECUTE
> cd /var/www/zm/events/6
>         $EXECUTE
> cd /var/www/zm/events/9
>         $EXECUTE
   ...

First, please make the $EXECUTE conditional on the success of the cd,
eg,
  cd /var/www/zm/events/1 &&
          $EXECUTE

Consider the potential disaster of a failed cd!!


Second, the find, as stated, will delete recursively all old
directories.  Since it is possible for an old directory to contain a
new file, is this what you really intend?  Perhaps this is what you
really want:

 EXECUTE="/usr/bin/find ./  -depth -type f ! -name "*.log" -mtime 5
   -exec rm -f {} \; "

This would remove only old files, visiting the subtree depth first.
To remove old directories as well, you'd want to use rmdir, instead of
rm, which will only remove empty directories.
Perhaps this would work:
 
  EXECUTE="/usr/bin/find ./  -depth ( -type f ! -name "*.log" -mtime 5
     -exec rm -f {} \; ) -o ( -type d  -mtime 5 -exec rmdir {} \; ) "


None of the above deals with other odd file types such as symlinks,
named pipes, sockets, etc., but you may not have to worry about them.


-- 
	David A. De Graaf    DATIX, Inc.    Hendersonville, NC
	dad at datix.us         www.datix.us



More information about the Ale mailing list