Mon May 19 17:01:00 UTC 2008

script to find and replace a string in all files and subdirectories using shell programming

Posted in Rails at 05:01 PM by matt

#!/bin/sh

if [ $# -ne 2 ]; then
        echo 1>&2 Usage: $0 [string to replace] [replacement]
        exit 127
fi

for file in $(find . -type f | grep -v .svn | grep -v .jpg | grep -v .gif | grep -v .tmp | grep -v .zip 
| grep -v .png | grep -v .ttf | grep -v .psd | grep -v tempfile.tmp)
do
        sed -e "s/$1/$2/g" $file > /tmp/tempfile.tmp
        mv /tmp/tempfile.tmp $file
done
for file in $(find . | grep -v .svn | grep tempfile.tmp)
do
        rm $file
done

Leave a Comment