opendir (D, "$somepath") || die "couldn't open dir $somepath"; my @txt_files = grep{/.txt$/} readdir D; #note that @txt_files only contains the file names, not #the full path...you will need "$somepath/$filename" in the open