Monday, March 2, 2015

Change a String in Every File in a Tree

Suppose I'd like to replace every 'string' with 'newstring' in the directory ~/directory:

Good:

find . -type f -print0 | xargs -0 sed -i 's/string/newstring/g'


Better:

grep -lrZ string | xargs -0 sed -i 's/string/newstring/g'


Best:

git grep -lz 'string' | xargs -0 sed -i 's/string/newstring/g'


Bester:

git grep -lz 'string' ~/directory | xargs -0 sed -i 's/string/newstring/g'


Here are some search terms that will help me find this:

how to change a string in every file in a tree
recursive editing
change strings in every file
find and grep and sed
git grep
how not to fuck up git when