opendir( D, "." ) or die "WTF? $!"; for my $file ( grep /\.txt$/, readdir D ) { # do whatever it is the OP is doing... }