foreach my $element($array){ open my $tempfile, '<', $element; foreach $line(<$tempfile>){ if ($line =~ $file_to_copy){ copy ($file_to_copy, "copied_files/$element) } else{ print ''; } }