Title: RE: [Perl-unix-users] Removing files older than 10 days from a directory

Here is a little something I threw together awhile ago to clean up old log and data files.
It's not fancy, but it works okay for me.

-- This call would remove files older than 30 days from the $root/log and $root/stage directories and
-- any subdirectories therein.
Purge( 30 ,[ "$root/log" ,"$root/stage"] );



#Take the number of days to retain data and the directory(ies) to purge.
sub Purge($$);
sub Purge($$)
{
   my ( $days ,$dirRef ) = @_;
   die "Must pass number of days and at least one directory, Purge stopped " if ( ! defined( $days ) || ! defined( $dirRef ) );

   #If this is only one directory, stick it in an array.
   $dirRef = [ $dirRef ] if ! ( ref($dirRef) =~ m/ARRAY/ );
   foreach my $dir ( @{$dirRef} )
   {
      my @allFiles = glob("$dir/*");
      foreach my $file ( @allFiles )
      {
         # If the $file is a directory, call Purge again and then skip checking the directory's age.
         if ( -d $file )
         {
            Purge( $days ,$file );
            next;
         }  
         if ( int( -M $file) > $days || $days == 0 )
         {
            unlink $file or die "Error removing $file\n$!, stopped";
         }
      }  
   }
return(0);
}



-----Original Message-----
From: Craig Sharp [mailto:[EMAIL PROTECTED]]
Sent: Friday, November 07, 2003 2:45 PM
To: [EMAIL PROTECTED]; [EMAIL PROTECTED]
Cc: [EMAIL PROTECTED]
Subject: RE: [Perl-unix-users] Removing files older than 10 days from a
directory


Travis,

The attachment did not come through.  Please try again or you can just
send it by email.

Thanks,

Craig

>>> Travis Hoyt <[EMAIL PROTECTED]> 11/07/03 01:57PM >>>

Here's our version for Oracle being backed up by Veritas NetBackup.

I tried to just cut and paste this but it isn't keeping the tabbing
right.  I'll upload it as a text attachment.

Our version is more complicated than previous versions offered but
there is a great deal more accounted for with threadholds, alerting and
other such items that are beneficial in a commercial environment.  If
you have any questions about the script please feel free to e-mail me.

Travis



_______________________________________________
Perl-Unix-Users mailing list
[EMAIL PROTECTED]
To unsubscribe: http://listserv.ActiveState.com/mailman/mysubs

Reply via email to