#! /usr/bin/perl use strict; use warnings; my $path = "./IN/"; my $string = 'ABC'; my @filesWithString; opendir (my $directory, $path) or die $!; while(readdir $directory) { next if $_ =~ m/^\./; my $fileName = "$path" . "$_"; open (my $checkForString, "<", "$fileName"); while (<$checkForString>) { print STDERR "$_"; if ($_ =~ m/$string/) { push (@filesWithString, $fileName); } } close $checkForString; } closedir $directory; for (@filesWithString) { my $newName = "$_".'.ABC.txt'; open (my $old, "<", $_); open (my $new, ">", $newName); while (<$old>) { print $new "$_"; } close $old; close $new; }