[TriLUG] Perl problem

Stephen Hoffman srhoffman at bellsouth.net
Tue Jun 8 16:17:26 EDT 2004


Having a problem with a perl script.  It is mainly to flush files out of a
directory once a nightly backup has occured.  So here's what it's SUPPOSED
to do.
   Look in a directory for files older then a certain number of days and
simply delete them.  At first glance it works quite well, but upon
further inspection I can't specify any other directory then the one
that the perl script resides in.  So where I have $dir="." that can be
$dir="/path/to/current/directory" and it still works, but if I specify
a directory elsewhere it claims that the files in that directory were
last modified on the Epoch date and are 12K+ days old.

Am I missing something?

Any help GREATLY appreciated.

Steve


#!/usr/bin/perl -w
use strict;

my $threshold = 1;
print "Deleting files older than $threshold days\n";

my $dir = ".";

opendir(D,$dir) or die $!;
my $time = time();

while (my $f = readdir(D)) {
    next if $f =~ /^\./;
    my ($atime, $mtime, $ctime) = (stat($f))[8..10];
    my $age_hours = ($time - $mtime) / 3600;
    my $age_days  = int($age_hours / 24);
    next unless $age_days > $threshold;
    print "---> Deleting $f ($age_days days)...";
####################    unlink $f;
    print " done\n";
}

closedir(D);



More information about the TriLUG mailing list