use strict; use warnings; use Tie::File; use File::Spec; use File::Copy qw(move); my $dir = 'test'; my $out = 'SNNHUB01'; opendir my $dirh, $dir or die $!; while( my $entry = readdir $dirh ){ my $path = File::Spec->catfile( $dir, $entry ); next unless -f $path; tie my @lines, 'Tie::File', $path or die $!; my $found = grep { /SNNH-UB01-002/ }@lines; untie @lines; next unless $found; my $new = File::Spec->catfile( $out, $entry ); move $path, $new or die $!; } closedir $dirh;