Recursive SED Script
There are often times I want to run a SED find and replace across recursive directories, and the below is a very handy script to do so, from this excellent resource. There’s plenty of other variations of it there too.
#SR does global replacement in html files, replacing its first parameter by
#its second parameter--use with extreme caution!
find ./ -name '*.html' -print | while read i
do
sed "s/$1/$2/g" $i >$i.bak && mv $i.bak $i
done
To use this, simply execute the command via:
./SR term_to_find term_to_replace_with
As a slight tweak, and what I find more useful, is to add a third input param, for example to switch on the file names you want to replace:
#SR does global replacement in files specified in STDIN as third parameter, replacing its first parameter by
#its second parameter--use with extreme caution!
find ./ -name "*.$3" -print | while read i
do
sed "s/$1/$2/g" $i >$i.bak && mv $i.bak $i
done
For example, to replace image1.jpg with image2.jpg, in all css files, run this:
./SR image1.jpg image2.jpg css
