?
Solved

find oldest file

Posted on 2004-11-27
4
Medium Priority
?
900 Views
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.

EX

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

thanks,

0
Comment
Question by:bt707
4 Comments
 
LVL 10

Accepted Solution

by:
rj2 earned 2000 total points
ID: 12686571
use File::Find;

$dir='/mail/queues';

find(\&wanted_files, $dir);

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

sub wanted_files{
      if(-f $_) {
            $File::Find::dir=~m![/\\]([^/\\]+)$!;
            $topdir=$1;
            if($topdir >= 1 && $topdir <= 20) {
                  #print "Checking dir $File::Find::dir ($topdir)\n";
                  $days=-M $File::Find::name;
                  if($days > $oldest) {
                        $oldestdays=$days;
                        $oldestname=$File::Find::name;
                  }            
            } else {
                  #print "Not checking dir $File::Find::dir ($topdir)\n";
            }      
      }
}
0
 
LVL 5

Expert Comment

by:roee_f
ID: 12687199
#!/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";
0
 
LVL 85

Expert Comment

by:ozo
ID: 12687506
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);
0
 

Author Comment

by:bt707
ID: 12688046
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,
0

Featured Post

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

On Microsoft Windows, if  when you click or type the name of a .pl file, you get an error "is not recognized as an internal or external command, operable program or batch file", then this means you do not have the .pl file extension associated with …
There are many situations when we need to display the data in sorted order. For example: Student details by name or by rank or by total marks etc. If you are working on data driven based projects then you will use sorting techniques very frequently.…
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…
Six Sigma Control Plans
Suggested Courses

840 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