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

I'm having some lovehate with File::Find::Rule.. Why doesn't this find empty dirs??
use File::Find::Rule; use Smart::Comments '###'; my $path = '/home/myself'; mkdir "$path/nothing"; my $finder= File::Find::Rule->new; $finder->directory(); $finder->empty(); my @found = $finder->in($path); ### @found
Is it because it's just testing for file (as in file, not dir) size?

update This seems to work :

use IO::Dir; use File::Find::Rule; sub _is_empty_dir { my ($shortname, $path, $fullname) = @_; my $dh = IO::Dir->new($fullname) or return; my $count = scalar(grep{!/^\.\.?$/} $dh->read()); $dh->close(); return($count==0); } sub emptydirfinder { my ($self,$abs) = shift; $abs or die(); my $finder= File::Find::Rule->new; $finder->directory(); $finder->exec( \&_is_empty_dir); my @found = $finder->in($abs); return \@found; }

Replies are listed 'Best First'.
Re: how to get File::Find::Rule to match empty directories
by runrig (Abbot) on May 25, 2007 at 06:02 UTC
    Yes, it's a "file empty", not "directory empty" test. It's the same as the '-z' file test operator.
Re: how to get File::Find::Rule to match empty directories
by ikegami (Patriarch) on May 25, 2007 at 06:22 UTC

    Except for root dirs, few dirs are truly empty anyway. They contain . and ...

      ... and even my root dir contains '.' and '..'...