use strict; use warnings; ## open the 'merge' for writing ## this way you will only wrote in the files from this merge ## and not keep anything that is already in there ## to keep any pre-existing content, leave as # open(my $merge_fh, '>>', $targetfile) || die "Failed to append to target: $!"; ## always check for success on open/close FH operations open(my $merge_fh, '>', $targetfile) || die "Failed open \'$targetfile\' for merging: $!"; my @rpts = glob('//testserver/dir/*'); foreach my $x (@rpts) { ## open each file to be merged in turn open (my $fh, '<', $x) || die "Failed open \'$x\' for reading : $!"; ## read out each line, printing it into the target file while (<$fh>){ print $merge_fh $_;} ## close up close $fh || die "Failed to close \'$x\' : $!"; } ## now all the files have been copied into the target, you are free to close it. close $merge_fh || die "Failed to close $targetfile after merge : $!";