Beefy Boxes and Bandwidth Generously Provided by pair Networks
The stupid question is the question not asked
 
PerlMonks  

Quickly checking if a directory is empty

by Anonymous Monk
on Aug 18, 2009 at 14:38 UTC ( [id://789479]=perlquestion: print w/replies, xml ) Need Help??

Anonymous Monk has asked for the wisdom of the Perl Monks concerning the following question:

I need to check and see if certain directories are empty (without deleting them) as part of a larger program and I use the code below. Some of the directories have a lot of files so I thought I would optimize it by including the last if line to keep it from counting all files but it makes no difference in the time this sample code takes to run? Note I have about 10,000 files in the directory I am testing this on and it takes about 4 seconds for the code to complete with or without the last if line.
$start = time(); chdir "c:/temp"; while (<*>) { $num++; last if ($num==1); # makes no difference in speed } $stop = time(); $elapsed=$stop-$start; print "elapsed = $elapsed seconds\n";
What am I missing and/or is there a better way to do this? Again, all I need to know is if the directory is empty or not without deleting anything.

Replies are listed 'Best First'.
Re: Quickly checking if a directory is empty
by Fletch (Bishop) on Aug 18, 2009 at 14:53 UTC

    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.

Re: Quickly checking if a directory is empty
by Your Mother (Archbishop) on Aug 18, 2009 at 15:45 UTC

    Path::Class offers a way. The module is portable across different file systems though the example is *nix. The main advantage to using this approach is it grows nicely and lets you pass things around easily.

    moo@cow~>mkdir temp moo@cow~>perl -MPath::Class -le 'print Path::Class::Dir->new("./temp") +->children == 0 ? "none" : "some"' none moo@cow~>touch temp/fish moo@cow~>perl -MPath::Class -le 'print Path::Class::Dir->new("./temp") +->children == 0 ? "none" : "some"' some
Re: Quickly checking if a directory is empty
by leocharre (Priest) on Aug 18, 2009 at 15:49 UTC
Re: Quickly checking if a directory is empty
by alexm (Chaplain) on Aug 18, 2009 at 20:55 UTC

    Using File::Slurp::read_dir() as in:

    use File::Slurp; if ( read_dir('/path/to/dir') == 0 ) { print "is empty!\n"; }

    Update: using File::Slurp or Path::Class will have bad performance on a directory with a large number of entries. However, File::Find::Rule::DirectoryEmpty is addressing the problem the right way, as mentioned by Fletch, by looking for any other entry aside from "." and ".." (which means 3 readdir calls at most whatever the number of entries).

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://789479]
Approved by rovf
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others taking refuge in the Monastery: (3)
As of 2024-04-19 22:20 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found