From: Peter Wyzl on 14 Nov 2007 08:14 "Steven M. O'Neill" <steveo(a)panix.com> wrote in message news:fhd74l$lls$1(a)reader1.panix.com... > Abigail <abigail(a)abigail.be> wrote: >> _ >>M (no(a)spam.tonsjunkmail.com) wrote on VCLXXXVII September MCMXCIII in >><URL:news:13jk6m2gka8l223(a)corp.supernews.com>: >>|| Is there an easy way to get perl to return the mtime of the oldest >>file in a >>|| directory? >> >> >>No. >> >>Reason #1 is that "oldest file" isn't clearly defined. Reason #2 is that >>for some definitions of "oldest", including the very obvious one, you need >>a piece of information that many filesystems don't keep track of. >> >>None of these reasons have anything to do with the language you try to >>solve the problem with. > > http://groups.google.com/group/comp.unix.shell/msg/777922ad16818eec?dmode=source&hl=en Wow, thats what? 14 years ago? Abigail's comments, albeit crytic are correct. Which definition of oldest do you require? Least recently modified? Least recently accessed? First created? Once you answer that, the rest is trivial with 'stat' and 'readdir'... P
From: Glenn Jackman on 14 Nov 2007 09:15 At 2007-11-13 04:44PM, "M" wrote: > Is there an easy way to get perl to return the mtime of the oldest file in a > directory? opendir my $dir, '.' or die "..."; my @files = readdir $dir; closedir $dir; my @with_mtime = map {[$_, (stat $_)[9]]} @files; my @sorted = sort {$a->[1] <=> $b->[1]} @with_mtime; my $oldest = $sorted[0][0]; You can avoid all the temp vars: opendir my $dir, "." or die "..."; my $oldest = [ sort {$a->[1] <=> $b->[1]} map {[$_, (stat $_)[9]]} readdir $dir ]->[0][0]; closedir $dir; -- Glenn Jackman "You can only be young once. But you can always be immature." -- Dave Barry
From: Ben Morrow on 14 Nov 2007 09:32
Quoth "Peter Wyzl" <wyzelli(a)yahoo.com.au>: > "Steven M. O'Neill" <steveo(a)panix.com> wrote in message > news:fhd74l$lls$1(a)reader1.panix.com... > > Abigail <abigail(a)abigail.be> wrote: > >> > >>Reason #1 is that "oldest file" isn't clearly defined. Reason #2 is that > >>for some definitions of "oldest", including the very obvious one, you need > >>a piece of information that many filesystems don't keep track of. <snip> > > Abigail's comments, albeit crytic are correct. Which definition of oldest > do you require? Least recently modified? Least recently accessed? First > created? > > Once you answer that, the rest is trivial with 'stat' and 'readdir'... Part of Abigail's point was that 'first created' is impossible to determine on many filesystems, as creation time is not recorded. Even when it is, Perl may not provide easy access to that information (BSD's 'birthtime', for instance). Ben |