简体   繁体   中英

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?

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]$  
sed --in-place 's/www.mysite.com/www.myNewSite.com/g' *.php

应该可以。

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM