By Alvin Alexander. Last updated: June 4, 2016
Perl has a couple of convenient file operators that let you determine when a file was last accessed or modified in units of days. These operators are:
-M The modification age of the file, in days -A The access age of the file, in days
A quick Perl file test example
You can use these Perl file test operators in your scripts to determine when files are getting "old" (where the definition of "old" is up to you).
Here's a quick example that determines whether a file has been modified in the last 90 days:
$filename = 'foo.dat'; # test the file modification time if (-M $filename > 90) { print "$filename has not been modified in at least 90 days\n"; }
Of course you can do the same thing with the file access time operator:
$filename = 'foo.dat'; # test the file access time if (-A $filename > 90) { print "$filename has not been accessed in at least 90 days\n"; }