find oldest file

Posted on 2004-11-27
Last Modified: 2012-06-27
I have a directory with about 20 sub directories in it that has a thousands of files in them all together, I need a script to find what is the oldest file and show date and time created in which ever one of these sub directories.


/mail/queues/(dir's 1-20)


Question by:bt707
    LVL 10

    Accepted Solution

    use File::Find;


    find(\&wanted_files, $dir);

    print "The oldest file is $oldestname, last modified $oldestdays days ago\n";

    sub wanted_files{
          if(-f $_) {
                if($topdir >= 1 && $topdir <= 20) {
                      #print "Checking dir $File::Find::dir ($topdir)\n";
                      $days=-M $File::Find::name;
                      if($days > $oldest) {
                } else {
                      #print "Not checking dir $File::Find::dir ($topdir)\n";
    LVL 5

    Expert Comment

    use strict;
    use warnings;
    use File::Find;

    @ARGV = qw(".") unless @ARGV;
    my $oldestName = "";
    my $oldestDate = 0;

    sub process_file {
          return unless -f $_ && -M $_ > $oldestDate;
        $oldestDate = -M $_ ;
        $oldestName = $File::Find::name;

    find(\&process_file, ".");

    my $age = (stat($oldestName))[9];
    print "Oldest file is $oldestName. Changed in ", scalar(localtime($age)), "\n";
    LVL 84

    Expert Comment

    If you want to restrict the find to /mail/queues/{1..20} out of possiblly other /mail/queues/* sub directories, other you can use
    find(\&process_file, map{"/mail/queues/$_"}1..20);

    Author Comment

    That one worked great I just didn't get back to accept it, others looks good but the first one worked and didn't get to try the others.


    Featured Post

    How your wiki can always stay up-to-date

    Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
    - Increase transparency
    - Onboard new hires faster
    - Access from mobile/offline

    Join & Write a Comment

    I've just discovered very important differences between Windows an Unix formats in Perl,at least 5.xx.. MOST IMPORTANT: Use Unix file format while saving Your script. otherwise it will have ^M s or smth likely weird in the EOL, Then DO NOT use m…
    Many time we need to work with multiple files all together. If its windows system then we can use some GUI based editor to accomplish our task. But what if you are on putty or have only CLI(Command Line Interface) as an option to  edit your files. I…
    Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
    how to add IIS SMTP to handle application/Scanner relays into office 365.

    755 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    20 Experts available now in Live!

    Get 1:1 Help Now