Here's a quick recipe to scan a directory recursively (aka "walking") for all files that meet a given criteria--in this case I'm searching for perl scripts. In fact, because this is Perl, I'll give you three ways to do it!
First we'll use the glob function, which returns a list of file and directory names. But, because that doesn't descend into the directories, we implement that ourselves, by recursively calling the same sub.
my $root = "/home/michael/sites";
rlist($root);
sub rlist {
my ($dir) = @_;
for (glob "$dir/*") {
-d and rlist($_);
/\.pl$/ and print "$_\n";
}
}If you're on a Unixish operationg system you have access the powerful find command, and with Perl's backtick operator, we can harness that for our own use. It's already recursive and can filter for things like files (vs directories).
for my $file (grep{m/\.pl$/}`find $root/ -type f -print`) {
print "$file";
}Finally, but probably the best way to do it, is Nicholas Clark's handy Find::File module. This has a few bells-and-whistles and is as cross-platform a solution as you are going to get. It takes a call-back reference to a sub routine, which it will call everytime it finds a file or directory.
use File::Find;
find({wanted => \&found}, $root);
sub found {
/\.pl$/ and print("$File::Find::name\n");
}