0

I am using Linux CentOS. I have many folders inside my www directory and there are a lot of files inside those folders. I would like to change in those files:

www.mysite.com

to

www.myNewSite.com

Is there a way to run q command and that will replace all?

hveiga
  • 6,725
  • 7
  • 54
  • 78
arlind
  • 165
  • 1
  • 3
  • 15
  • This is a tad beyond my level of script-fu but take a look at the sed command http://stackoverflow.com/questions/5171901/sed-command-find-and-replace-in-file-and-overwrite-file-doesnt-work-it-empties – Jason Sperske Jun 21 '15 at 15:11
  • Also, please do remember to accept your answers (all of them) if they have given you satisfactory answers (by clicking on the tick mark on the left of the answers), it would be helpful to the community – TheCodingFrog Jun 27 '15 at 16:13

3 Answers3

2

You can use sed command. Below is the command. I tested and seems working.

[chatar@/Users/chatar]$  find test -name '*.php' 
test/folder1/one.php
test/folder1/two.php
test/folder2/four.php
test/folder2/three.php
[chatar@/Users/chatar]$  find test -type f -name '*.php' -exec grep www {} \;
www.mysite.com 
www.mysite.com 
www.mysite.com 
www.mysite.com 
[chatar@/Users/chatar]$  find test -type f -name '*.php' -exec sed -i -e 's/mysite/myNewSite/g' {} \;
[chatar@/Users/chatar]$  find test -type f -name '*.php' -exec grep www {} \;
www.myNewSite.com 
www.myNewSite.com 
www.myNewSite.com 
www.myNewSite.com 
[chatar@/Users/chatar]$  
TheCodingFrog
  • 3,406
  • 3
  • 21
  • 27
0
sed --in-place 's/www.mysite.com/www.myNewSite.com/g' *.php

should do the trick.

cmaster - reinstate monica
  • 38,891
  • 9
  • 62
  • 106
0

You can also use a Perl on-liner:

perl -pi -e 's/www\.mysite\.com/www.myNewSite.com/g' *.php

If you want to keep copies of the original files (with .bak extensions), use:

perl -pi.bak -e 's/www\.mysite\.com/www.myNewSite.com/g' *.php
davidedb
  • 867
  • 5
  • 12