use File::Copy; my ($filetobecopied,$newfile); system('ls -lrt | tail -15 | grep working > temp.txt'); open(FH,"final.txt"); while() { my $line = $_; if($line=~/.*(L_.*)(\d{1})(\.txt)(\.working)/) { # print "$1 - $2 - $3 - $4 \n"; my $one = $1; my $two = $2; my $three = $3; my $four = $4; my $val = 1; #print "$one 1 $three \n"; my $append = "$one" . '1' . "$three"; print "========================================== \n"; print "$append \n"; $filetobecopied = $append; $newfile = "/u/mindspring/data/fulfillment/FSI/outgoing/ $append"; copy("$filetobecopied","$newfile") or die "File cannot be copied."; # The path of the existing directory where the file L_push_201207191101.txt to be copied is /u/mindspring/data/fulfillment/FSI/outgoing } }