title: Perl: Fastest way to find files older than X number of minutes, sorted oldest to newest?
body: <p>=================</p>
  
  <p><strong>1. Find the files older than X number of minutes</strong></p>
  
  <p><strong>2. Process them from oldest to newest</strong></p>
  
  <p>The Code below works fine, however the directory contains 3 millions of files. Hence I need to optimize it to find the files faster. I don't have to worry about the content of the file just the name. </p>
  
  <pre><code>###########################
  sub get_files_to_process{
  ###########################
  # Declare arrays
  my @xmlfiles;
  my @qulfiedfiles; 
  
  # Declare a Dictionary
  my %filedisc;
  
  opendir(my $dh, $maindir) or die "opendir($maindir): $!";
  
   # Read all the files
   while (my $de = readdir($dh)) {
      # get the Full path of the file
      my $f = $maindir . $de;
      # If File is there and has .xml Extension
      if ( -f $f &amp;&amp; $f=~ /\.xml/){
         # Put it in a XMLFILES Array
         push (@xmlfiles, $f); }
      }
      closedir($dh);
  
  
   # For every file in directory
   for my $file (@xmlfiles) {
  
      # Get stats about a file
      my @stats = stat($file);
  
      # If time stamp is older than minutes provided
      if ($stats[9] &lt;= ($now - (( $minutesold * 60) ))){
  
         # Put the File and Time stamp in the dictionary
         $filedisc{$file} = $stats[9];
      }
   }
  
  # For every file in the dictionary sort based on the timestamp oldest files first
   for my $x (sort {$filedisc{$a} &lt;=&gt; $filedisc{$b} or $a cmp $b } keys %filedisc) {
  
      # Put the qualified files (Based on the age) in a list
         push(@qulfiedfiles, $x);}
  </code></pre>
  
  <p><strong>UPDATE: So far this seems promising, more testing to be done:</strong> </p>
  
  <pre><code>##########################
  sub get_files_count{
  ##########################
  
     my $cmd= "find $maindir -maxdepth 1 -name '*.xml' -mmin +$minutesold -printf \"%T+\t%p\\n\"| sort";
     my @output = `$cmd`;
  
     if (@output){
        foreach my $line (@output){
              chomp $line;
              push (@files2process, ( split '\t', $line )[ -1 ]);
           }
        }
     }
  </code></pre>