Link to home
Start Free TrialLog in
Avatar of bt707
bt707Flag for United States of America

asked on

find oldest file

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.

EX

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

thanks,

ASKER CERTIFIED SOLUTION
Avatar of rj2
rj2

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of roee_f
roee_f

#!/usr/bin/perl
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";
Avatar of ozo
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);
Avatar of bt707

ASKER

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.


Thanks,