art
art

Reputation: 175

Linux replace a word in all files

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?

Upvotes: 0

Views: 96

Answers (3)

davidedb
davidedb

Reputation: 876

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

Upvotes: 0

sed --in-place 's/www.mysite.com/www.myNewSite.com/g' *.php

should do the trick.

Upvotes: 0

TheCodingFrog
TheCodingFrog

Reputation: 3514

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]$  

Upvotes: 2

Related Questions