my @FILES = grep { -f } readdir (TEMP); foreach my $file (@FILES){ open (F, $file) or warn "Couldn't open $file: $!" and next; my @matches = grep { /$str/ } ; close (F); next unless scalar @matches > 0; print join("\n", @matches); } #### print $matches[0], "\n";