How to replace text in files from Linux command line

Little one liner to find and replace all occurrences of the string in all files of certain type, recursively.

find . -name "*.TYPE_OF_FILE" -print | xargs sed -i 's/SEARCH_PHRASE/REPLACE_PHRASE/g'

e.g. if you want to change “some_annoying_variable” to “some_var” in all .php files, the command will be:

find . -name "*.php" -print | xargs sed -i 's/some_annoying_variable/some_var/g'

Leave a Reply

Your email address will not be published. Required fields are marked *