use warnings; use strict; use File::Copy; my $write_dir = 'two/'; my @files = ; for my $file (@files){ open my $fh, '<', $file or die "can't open file $file for reading!: $!"; my $copy_it = 0; while (my $line = <$fh>){ if ($line =~ /keyword/){ print "$file\n"; $copy_it = 1; last; } } close $fh; if ($copy_it){ copy $file, $write_dir or die "can't copy file $file to $write_dir!: $!"; } }