String replace in multiple files using a recursive file search

Here is a command line method to replace a string in multiple files in the current and sub directories.

grep -rli 'old string' --include=*.php | xargs -i@ sed -i 's/old string/new string/g' @

This basically asks grep to do a recursive search and list all the php files in the current directory and all sub-directories with the string – ‘old string

Then on each file in the list, you are using sed to replace the string ‘old string‘ with ‘new string

You can include more file types in the grep search by changing the include flag to something like --include={*.php,*.js,*.html}

One thought on “String replace in multiple files using a recursive file search

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s