Tonight we were switching a clients WordPress site to a new domain name and in the process found some of the old domain was hard coded into PHP files.  There were too many to try and sort through them, plus I was likely to miss something.  I knew a script was needed.  My Google-fu was weak, I tried some posted tips on how to do this using perl and php commands but found they didn’t work in our environment.  Finally I remembered a tool we used to search and insert scripts in the hosted PBX environment I used to maintain.  sed.  I found good old sed worked best for this:

sed -i ‘s/OLD TEXT TO REPLACE/NEW TEXT TO REPLACE/g’ *.php

Try it out for yourself, and of course an obligatory shout out to my cousin Gabe Anderson for posting a perl script that worked for him 4 years ago.

Tagged with:
 

One Response to Find and Replace Text in Multiple Files – Linux Command Line HOWTO

  1. Thanks for the shout-out, Lane! I posted the original version of that blog entry in 2003, so glad it’s had such lasting power.

Leave a Reply

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

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>