Skip to content

Instantly share code, notes, and snippets.

@jamesnesfield
Created July 18, 2013 11:48
Show Gist options
  • Save jamesnesfield/6028699 to your computer and use it in GitHub Desktop.
Save jamesnesfield/6028699 to your computer and use it in GitHub Desktop.
a multi-file hierarchical "find and replace" that plays nice with spaces in names.
#!/bin/bash
startdirectory="/Users/jamesnesfield/Desktop/replaceTest/"
searchterm="foo"
replaceterm="bar"
i=0
IFS=$'\n';
for file in $( grep -l -R $searchterm $startdirectory)
do
((i++))
echo $file
sed -e "s/$searchterm/$replaceterm/g" "$file" > /tmp/tempfile.tmp
mv /tmp/tempfile.tmp $file
done
echo "done. changed $i files."
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment