use strict; use warnings; my $inDir ="."; my @inFiles = glob ("$inDir/*.txt"}; foreach my $infile (@inFiles) { # read infile into memory.. open IN, '<', "$inDir/$infile" or die "$!"; my @lines = <$infile>; close IN; # save the original infile rename ("$inDir/$infile", "$inDir/$infile-backup") or die "$!"; # modify the data that's in memory unshift @lines, "whatever_you_want\n"; #save the new data into the original file name open (OUT, '>', "$inDir/$infile") or die "$!"; print OUT @lines; close OUT; }