Pages

Wednesday, April 7, 2010

Perl:: deleting log files older than 'N' days

#!/usr/bin/perl -w

use strict;

# Script to delete the files older than "N" days.

# The directory to be cleaned up.(e.g ' ' means current directory)

my $DIR='';

# The age of file to be deleted. (e.g if given 2, files older than two days will get deleted)

my $life=2;

# File format to be matched can be enhanced. formats are seperated by whitespace

foreach my $t (glob("$DIR*.pl_* $DIR*stc $DIR*tcl $DIR*topo"))

{

my $age =int(-M $t);

if($age > $life)

{

print "$t is " . $age . " days old. Hence deleting it...\n";

unlink $t;

}

}

No comments:

Post a Comment