How can I replace a string of text in a whole series php files throughout a series of folders and sub-folders?

Hi,
My linux skills are pretty limited to "apt-get install"ing and "| grep"ing.
I have a pressing project that's lagging behind as I wait for my web developer to change the references in his php files to read from a remote mysql host instead of "localhost"
I can't afford to wait much longer, so I would like a backup option to do it myself if need be.
How can I from the command line replace "mysql_connect('localhost',"  with "mysql_connect('mynewhost'," in all the php files in my /var/www/virtual and all it's sub-folders?
Thanks
Steven
StevenHookAsked:
Who is Participating?
 
DustinKikuchiCommented:
rpl -x'.php' -R "mysql_connect('localhost'," "mysql_connect('mynewhost'," *

Open in new window


you can use -sR in order to use simulation mode recursively which will just list the files that will be affected.  Or -svR to have it do so with the verbose option to list the directory, file, and lines that will be affected.

Edit to add:  Agree with the above, however.  Ensure you have a backup just in case!
1
 
farzanjCommented:
I hope there is NO spaces in the file names or any special characters.

PREPARATION:
STEP 1:
First step-- make a backup

cp -pr /var/www/virtual somelocation

Step 2:
Check that the bakcup is done correctly.
0
 
farzanjCommented:
My offering:

find /var/www/virtual -type f -name '*php' -exec sed -i "s/mysql_connect('localhost/mysql_connect('mynewhost/g" {} \;

Open in new window

0
Ultimate Tool Kit for Technology Solution Provider

Broken down into practical pointers and step-by-step instructions, the IT Service Excellence Tool Kit delivers expert advice for technology solution providers. Get your free copy now.

 
StevenHookAuthor Commented:
there are def spaces in many of the file  and folder names.
Thanks for the advice.
What's the -pr option for in cp?
Thanks
Steven
0
 
farzanjCommented:
p  to preserve permissions of files
r   recursively through folders.

It still should work fine.  If you want to test first, remove -i from the sed command, that way it would show the modified files and not change them.
0
 
DustinKikuchiCommented:
With regard to using find/sed, I would suggest using xargs instead of exec as it will fork fewer times leading to faster execution.  Probably not very noticeable if you don't have many files and likely not a big deal for a one-time operation like this but always good to be thorough.

find /var/www/virtual -type f -name '*php' | xargs sed -i "s/mysql_connect('localhost/mysql_connect('mynewhost/g"

Open in new window

0
 
StevenHookAuthor Commented:
There are probably about 20k files, possibly more.....
so making it more efficient would help I think :)

would the find & sed method be better than rpl? rpl seems so simple straight forward - I understand the syntax.

I am busy running a backup, I used rsync and included only .php files because the entire directory structure is several TB and I don't have anywhere to put it fast right now.



Steve
0
 
DustinKikuchiCommented:
I find rpl to just be more straight forward and thus it's my goto.  Honestly, I haven't done a comparison as to its efficiency compared to find since I've never had the need to do such a large replace.  The execution times shouldn't be astronomical anyway.  When running find/exec on a couple thousand files execution times may be 5-10 seconds where xargs will bring it down to 2-3.  As a percentage it's a huge efficiency gain but in practice even with your 20,000 files we're talking about 50-100 seconds compared to 20-30 (obviously just examples and file size etc will make a difference) -- at the end of the day you likely won't really miss the extra minute it may take.  That's why I end up going for the most straight forward approach.

Here's the full documentation on rpl: http://www.laffeycomputer.com/rpl.html
Note that you may need to get the package.
0
 
StevenHookAuthor Commented:
Worked great thanks.
Had to cd to the main folder first....
Steven
0
 
StevenHookAuthor Commented:
Hi,
rpl -x'.php' -R "mysql_connect('localhost'," "mysql_connect('mynewhost'," *

Open in new window

Worked great.
This time I have an " in my string that must be replaced.
How do I replace things with "s in it? do they need to be escaped?
I wanted to confirm before running this:
 rpl -x'.php' -R "$hostname = "localhost";" "$hostname = "mdb.rapidstudio.co.za";" *

Open in new window

Thanks
Steven
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.