for (my $i=0; $i < 5; $i++){ opendir(DIR,$i) or die(); foreach my $file (readdir(DIR)) { # if the filename ends in "test.txt"... # adjust the regex as needed if ($file =~ /test\.txt$/) { print "$i, -${file}-\n"; } } closedir(DIR); }