For when you only want the top few subdirectories in a tree
#!/usr/bin/perl -w use strict; use File::Find; @ARGV = "." unless @ARGV; my $depth = 2; # or whatever makes sense sub showdirs { # line altered from original as per merlyn's # advice below (thanks!) $File::Find::prune = 1 if $File::Find::name =~ tr/\/// >= $depth; # do something, like this if you just want to print the subdirecto +ries print "$File::Find::name/\n" if -d; } find (\&showdirs, @ARGV);

Replies are listed 'Best First'.
RE: Controlling depth with File::Find
by merlyn (Sage) on Oct 13, 2000 at 10:49 UTC
    For safety, you might want to change that middle line to:
    $File::Find::prune = 1 if $File::Find::name =~ tr/\/// >= $depth;
    Note the safer use of >=, which works even if you're already below the target. Defensive programming can save much pain.

    -- Randal L. Schwartz, Perl hacker