Here's a piece of kit from my personal toolbox. I'm a Unix sysadmin and I sometimes run into directories with millions of files - such as mail spools choking with spam. The system tools will regularly choke on these. I've used these tools with great success. At one site I used these to clean up spam-infested mail queues when the built-in tools of a major commercial MTA weren't fast enough.

Sometimes the only thing of interest is the file count:

use strict; use warnings; my $dir = shift; die "Usage: $0 directory" unless defined $dir; opendir DIR, "$dir" or die "Could not open $dir: $!\n"; my @files=readdir DIR; print $#files+1, "\n"; closedir DIR;

Sometimes you need to search for files with some criteria and do something to them. Here's a script that searches for files based on name given a directory and regexp:
use strict; use warnings; my $dir = shift; my $criteria = shift; $criteria = "" unless defined $criteria; die "Usage: $0 directory" unless defined $dir; opendir DIR, "$dir" or die "Could not open $dir: $!\n"; my @files=grep(/$criteria/, readdir DIR); print $#files+1, " files\n"; chdir $dir; foreach my $file (@files) { # actions go here } closedir DIR;
The logic here is that this only picks the relevant files from the directory array, which is a reasonably fast operation even with a million-row array, and only touches the files in the result set. So if you have a directory with a million files, and there's one file you want to know about, you don't need to run through the other files going "no, that's not it..."

You'll notice a certain toollike air about these scripts, as if they were covered in grease, scratched and dinged from being thrown about in a toolbox. This is exactly the case. These get copied or rewritten every so often into random systems that don't necessarily have any CPAN modules or any practical way to install them in any reasonable amount of time. That's why the Spartan interface and simple structure. I've written a fancier version with options, lots of features, and nicely formatted output, but that's apparently left that behind on a previous employer's server. Dang.


In reply to Managing a directory with millions of files by jsiren

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.