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

In reply to Re: Quickest way to get the oldest file by mr_mischief
in thread Quickest way to get the oldest file by Anonymous Monk

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.