use strict; use warnings; use File::Find; sub wanted { my $file = $File::Find::name; print "$File::Find::name is a ", -d $file ? 'directory' : 'file', $/; } sub filter { grep { my $file = "$File::Find::dir/$_"; if (-d $file) { my $depth = $file =~ s#/#/#g; if ($depth > 3) { print "Depth > 3 in $file ... skipping\n"; 0; # return as grep result for this $file } else { 1; # concider this to recurse into } } else { 1; # include non-dirs } } grep !/^\.\.?$/, @_; } find( { no_chdir => 1, wanted => \&wanted, preprocess => \&filter, }, shift );