Find and substitute the string in all files with `sed` command on GNU/Linuxedit24 May 2016
Small and powerful command to find and substitute the
old_phrase with the
all files and directories recursively with
sed command on GNU/Linux
find . -type f -print0 | xargs -0 sed -i 's/old_phrase/new_phrase/g'
Attention! The previous command finds files also in the hidden folders and if you’re working with Subversion or GIT you’d like to skip them. The following keys
-not -path '*/\.*' makes the trick
find . -not -path '*/\.*' -type f -print0 | xargs -0 sed -i 's/old_phrase/new_phrase/g'
For MacOS users the command would be, note the
sed -i ''
find . -not -path '*/\.*' -type f -print0 | xargs -0 sed -i '' 's/old_phrase/new_phrase/g'
Note. MacOS ships the BSD sed. In GNU/Linux you run the GNU
sed. More around it https://stackoverflow.com/questions/7573368/in-place-edits-with-sed-on-os-x.
And checkout a beautiful visual explanation of shell commands.