Friday 6 March 2009

clean up files older than a week

#!/usr/bin/perl
use strict;

my $TODAY = time;
my $BACKUP_DIR = "/home/sriram/backup";
my $LOGFILE = "$ENV{HOME}/logs/cleanup.log";
my $wktime = 604800; #(ie 86400*7)

sub remove_old_files

{

open (LOGFILE, ">$LOGFILE");
opendir(DIR, $BACKUP_DIR) or die "could not open directory: $!";

while (my $file = readdir DIR){
next if -d "$DIR/$file";
my $mtime = (stat "$DIR/$file")[9];
if ($TODAY - $wktime > $mtime){

print LOGFILE "$DIR/$file is older than 7 days...removing\n";
unlink $file;
}
}
}

close LOGFILE;
close DIR;

sub main()
{
remove_old_files();
}

main();

No comments:

Post a Comment

Tweets by @sriramperumalla