Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
?
Solved

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

Posted on 2012-08-15
10
Medium Priority
?
545 Views
Last Modified: 2013-03-13
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
0
Comment
Question by:StevenHook
  • 4
  • 3
  • 3
10 Comments
 
LVL 31

Expert Comment

by:farzanj
ID: 38296152
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
 
LVL 4

Accepted Solution

by:
DustinKikuchi earned 2000 total points
ID: 38296156
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
 
LVL 31

Expert Comment

by:farzanj
ID: 38296192
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
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 

Author Comment

by:StevenHook
ID: 38296230
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
 
LVL 31

Expert Comment

by:farzanj
ID: 38296241
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
 
LVL 4

Expert Comment

by:DustinKikuchi
ID: 38296286
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
 

Author Comment

by:StevenHook
ID: 38296360
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
 
LVL 4

Expert Comment

by:DustinKikuchi
ID: 38296465
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
 

Author Closing Comment

by:StevenHook
ID: 38300720
Worked great thanks.
Had to cd to the main folder first....
Steven
0
 

Author Comment

by:StevenHook
ID: 38980270
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

Featured Post

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Since pre-biblical times, humans have sought ways to keep secrets, and share the secrets selectively.  This article explores the ways PHP can be used to hide and encrypt information.
This article discusses how to implement server side field validation and display customized error messages to the client.
The viewer will learn how to dynamically set the form action using jQuery.
This demo shows you how to set up the containerized NetScaler CPX with NetScaler Management and Analytics System in a non-routable Mesos/Marathon environment for use with Micro-Services applications.
Suggested Courses

572 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question