in reply to Deleting files based on date

Here's one way,

my $days = 30; for (glob '*') { unlink if -f and -M > $days; }
That uses the file test operators to check if each name rperesents an ordinary file, and to see if the file is more than 30 days old. Adjust $days (can be fractional) for age, and the argument of glob for path and any specializations like file extension.

After Compline,
Zaxo

Replies are listed 'Best First'.
Re: Re: Deleting files based on date
by DrHyde (Prior) on Oct 20, 2003 at 07:53 UTC
    I don't know why, but I find -M and friends confusing, because they work the 'wrong' way for my brane, by making older files have higher values, whereas stat and friends use epoch time and newer files have higher values. So I'd use stat like below. There's a little more code, but it's easier for me to read:
    unlink grep { (stat($_))[9] < time - ($days * 86400) } @files;
    where 86400 is the number of seconds in a day, so we unlink all files from the @files array where their modification time earlier than $days days old.
      ...I find -M and friends confusing, because they work the 'wrong' way for my brane...

      I agree, but for a different reason: the fact that calculations are done from script startup time can lead to unexpected results for long-running scripts, or when running under mod_perl.

      That's why I personally prefer to use stat() as well: changing $^T is too much "action at a distance" to me.

      On the other hand, being able to recognize 86400 as a synonym for a day, is a typicial sysadmin deformity to me ;-)

      Liz