Cookie Consent by Free Privacy Policy Generator Find and substitute the string in all files with sed command on GNU/Linux | Igor Moiseev

Igor Moiseev Applied mathematician, Web Developer

Find and substitute the string in all files with `sed` command on GNU/Linuxedit

Small and powerful command to find and substitute the old_phrase with the new_phrase in 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.

moiseevigor.github.io is a participant in the Amazon Services LLC Associates Program, an affiliate advertising program designed to provide a means to earn fees when linking to Amazon.com and affiliated sites.