in reply to Quickest way to get the oldest file

You'd want to check the modification time or date for all the files. Then you'd want to do a typical minimum function on the epoch times at which they were modified (using data from stat()) or a maximum function on the number of days since the file was last modified (using data from -M for example). Since the -X file test ops handle partial days, I usually just use those instead of stat if I only need one value.

A quick and dirty test for the oldest file in a directory follows. It currently makes no check to see if it's pointing to a regular file or something else (socket, directory, etc.). It just prints the name of the oldest file and the number of days since it was modified. I tested it a little and it seems to work.

use strict; use warnings; my $max = 0; my ( $time, $oldest ); opendir ( my $d, '.' ) || die "Can't get file list: $!\n"; my @file = readdir ( $d ); closedir ( $d ); foreach ( @file ) { next if /^\.{1,2}$/; $time = -M $_; if ( $time > $max ) { $max = $time; $oldest = $_; } } print "$oldest was modified $max days ago\n";


Christopher E. Stith