#!/usr/bin/perl
use warnings;
use strict;
use File::Find;
use Tie::File;
find (\&replace,"/path/to/directory");
sub replace {
my $cur_file=$File::Find::name;
return if (!-f $cur_file || !-w $cur_file);
tie my @file_content,'Tie::File',$cur_file
or die "Couldn't tie file $cur_file";
for (@file_content) {
s/this phrase/that locution/g;
}
}
You may want to read the Tie::File perldoc regarding file locking and memory consumption.
And just for completeness, here's a solution that does it all without perl:
find this/ -type f -exec sed -i 's/gg/dd/g' {} \;
Debugging is twice as hard as writing the code in the first place. Therefore, if you write the code as cleverly as possible, you are, by definition, not smart enough to debug it. -- Brian W. Kernighan
|