my $root_dir = '/top/directory/to/start/search'; RunDirs($root_dir); sub RunDirs { my $dir = shift; opendir(TMP_D,"$dir") or die "$! opening $dir"; while(my $tmp = readdir(TMP_D)) { $tmp = "$dir/$tmp"; if ($tmp =~ /\.\.?/) { next; } if (-d $tmp) { RunDirs("$tmp"); } elsif ($tmp =~ /^(\w*)\.(jpg|jpeg)$/) { print "$tmp\n"; } } closedir(TMP_D); }