Use this script
You owe me NOTHING!
sub replace;
$recursive = 0;
sub Printmenu {
print "Usage is: recureplace [option] 'find text' 'replace text' file1 file2 ... \n\n";
print " Replace the specified text in the specified files.\n\n";
print " options are:\n";
print " -e file_extension to replace word only in files matching the\n";
print " extension\n";
print " -R Recursive Perform recursively the replacement on all the\n";
print " files of all directories\n";
exit;
}
while ($ARGV[0] =~ /^-/) {
if ($ARGV[0] eq "-R") { $recursive = 1;}
elsif ($ARGV[0] eq "-e") { shift; $extension = $ARGV[0];}
shift;
}
if (($ARGV[0] eq "") ||
($ARGV[1] eq "")) { &Printmenu;}
$find_txt = $ARGV[0]; shift;
$replace_txt = $ARGV[0]; shift;
&find('.');
[zeus@baileys (Aug 06 - 3:17am) - /home/zeus] ls -la /usr/local/bin/rep
-rwxr-xr-x 1 root wheel 2386 Jul 1 2002 /usr/local/bin/rep
[zeus@baileys (Aug 06 - 3:17am) - /home/zeus] more /usr/local/bin/rep
#!/usr/bin/perl
sub replace;
$recursive = 0;
sub Printmenu {
print "Usage is: recureplace [option] 'find text' 'replace text' file1 file2 ... \n\n";
print " Replace the specified text in the specified files.\n\n";
print " options are:\n";
print " -e file_extension to replace word only in files matching the\n";
print " extension\n";
print " -R Recursive Perform recursively the replacement on all the\n";
print " files of all directories\n";
exit;
}
while ($ARGV[0] =~ /^-/) {
if ($ARGV[0] eq "-R") { $recursive = 1;}
elsif ($ARGV[0] eq "-e") { shift; $extension = $ARGV[0];}
shift;
}
if (($ARGV[0] eq "") ||
($ARGV[1] eq "")) { &Printmenu;}
$find_txt = $ARGV[0]; shift;
$replace_txt = $ARGV[0]; shift;
&find('.');
sub find {
local($dir,$nlink) = @_;
local($dev,$ino,$mode,$subcount);
($dev,$ino,$mode,$nlink) = stat ('.') unless $nlink;
opendir(DIR,'.')|| die "Can't open $dir";
local(@filenames)= readdir(DIR);
closedir(DIR);
if (!($recursive)) {
@filenames = @ARGV;
}
$subcount = $nlink - 1;
for (@filenames) {
next if $_ eq '.';
next if $_ eq '..';
$name= $_;
if (!(-d $name)){
if ($extension ne '') {
next if (!(m/.*\.$extension$/i))
}
replace($name);
}
next if $subcount hahahaha0;
($dev,$ino,$mode,$nlink) = lstat($_);
next unless -d _;
if ($recursive) {
print "\nDirectory: $dir/$name\n";
chdir $_ || die "Can't cd to $name";
&find($name,$nlink);
chdir '..';
--$subcount;
}
}
}
sub replace {
local ($filename)= @_;
if ((-r $filename) && (-w $filename) && (-s $filename)) {
$size = (-s $filename);
print "Replacing $find_txt by $replace_txt in $filename ($size)\n";
open(FILE,"<$filename");
read(FILE, $file_text, $size, 0);
close(FILE);
$file_text =~ s/$find_txt/$replace_txt/g;
open(FILE,">$filename") || die "Could not create $filename\n";
print FILE $file_text;
close(FILE);
}
else {
print "I have no right to modify $filename";
}
}