The way to find and Replace script for Linux
Posted by Unknown at 02:10Shell Script to Recursively Search and Replace on Linux
Save the file in the directory you want to search and call it replace.
—->>>do not add this line
#!/bin/bash
# This script will search and replace all regular files for a string
# supplied by the user and replace it with another string.
#
#
function usage {
echo “”
echo “Search/replace script revised by dwhs.net and ezliuxadmin.com”
echo “”
echo “Not enough parameters provided.”
echo “Usage: ./$0 searchstring replacestring”
echo “Remember to escape any special characters in the searchstring or the replacestring”
echo “”
}
#check for required parameters
if [ ${#1} -gt 0 ] && [ ${#2} -gt 0 ];
then
for f in `find -type f`;
do
if grep -q $1 $f;
then
cp $f $f.bak
echo “The string $1 will be replaced with $2 in $f”
sed s/$1/$2/g < $f.bak > $f
rm $f.bak
fi
done
else
#print usage informamtion
usage
fi
—->>>do not add this line
When you add the script make the permissions to 755
Then run the command string to set the variables.
./replace oldtext newtext
Subscribe to:
Post Comments (Atom)
0 comments:
Post a Comment