Search on an entire directory, finding all the instances of the text "findtext"
find . -type f -exec grep "findtext" {} \; -print
find . -type f -print0 | xargs -0 sed -i 's/replacewith/whatever/g'
#!/bin/sh
for file in $(grep -il "Hello" *.txt)
do
sed -e "s/Hello/Goodbye/ig" $file > /tmp/tempfile.tmp
mv /tmp/tempfile.tmp $file
done
Find and Replace
#!/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"
echo " Written by Sachchida ojha"
echo " sojha@secure-elements.com"
echo " http://www.secure-elements.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
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
done
else
#print usage informamtion
usage
fi