Skip to content

Instantly share code, notes, and snippets.

@mhewedy
Last active August 29, 2015 14:01
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mhewedy/dab0116aa288f67d05ba to your computer and use it in GitHub Desktop.
Save mhewedy/dab0116aa288f67d05ba to your computer and use it in GitHub Desktop.
bash file to refactor (find and replace)
#!/bin/bash
##
if [ $# -lt 2 ]
then
echo "usage $0 <old> <new>"
exit -1
fi
old=$1
new=$2
#find in files
files=`grep -HR $old * |grep -v 'Binary file'| cut -d':' -f 1|sort|uniq`
for f in $files
do
file $f >/dev/null
if [ $? == 0 ]
then
echo "processing $f"
sed -i s/$old/$new/g $f
fi
done
# find files with same name and rename files
for f in `find ./ -name $old`
do
newfile=`dirname $f`/$new
echo "Renaming $f to $newfile"
mv $f $newfile
done
echo "done"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment