linux admin php

How to search and replace text in multiple files in linux

Here's how to replace a string of text in multiple files under a specific directory and all it's sub directories.

cd /to/directory/containing/files
find . -name '*.php' | xargs perl -pi -e "s/OLDSTRING/'NEWSTRING'/g;"

Real world example:

Let's say we want to replace the following in every .php file:

'profile', 'load_profile'

with new string

'profile_load_profile'

Hence, we do the following...

cd /home
find ./ -name '*.php' | xargs perl -pi -e "s/'profile', 'load_profile'/'profile_load_profile'/g;"

Tags: linux admin php