Rather than using glob which probably is going ahead and reading the entire directory under the covers, you might try just calling opendir and then check if readdir returns more than 2 items (last'ing out on the 3rd; presuming of course a POSIX-y at least "." and ".." even in an empty dir). Alternately stat the directory itself and see if the nlink field is greater than 2.

And as a somewhat related aside: don't put thousands of files in a single directory if you can avoid it as for most filesystems it'll cause performance problems (as you've found). Break up files into groups (e.g. based on part of the filename, or artificially generate a key (Digest::MD5 is useful) and break that key up (directories 00, 01, ..., fe, ff, files with md5s starting with ab go in the ab directory, yadda yadda yadda)).

The cake is a lie.
The cake is a lie.
The cake is a lie.


In reply to Re: Quickly checking if a directory is empty by Fletch
in thread Quickly checking if a directory is empty by Anonymous Monk

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.