How to do a recursive find/replace of a string wit

2018-12-31 14:41发布

How do I find and replace every occurrence of:

subdomainA.example.com

with

subdomainB.example.com

in every text file under the /home/www/ directory tree recursively?

30条回答
梦寄多情
2楼-- · 2018-12-31 15:12

I just use tops:

find . -name '*.[c|cc|cp|cpp|m|mm|h]' -print0 |  xargs -0 tops -verbose  replace "verify_noerr(<b args>)" with "__Verify_noErr(<args>)" \
replace "check(<b args>)" with "__Check(<args>)" 
查看更多
栀子花@的思念
3楼-- · 2018-12-31 15:13

Here's a version that should be more general than most; it doesn't require find (using du instead), for instance. It does require xargs, which are only found in some versions of Plan 9 (like 9front).

 du -a | awk -F' '  '{ print $2 }' | xargs sed -i -e 's/subdomainA\.example\.com/subdomainB.example.com/g'

If you want to add filters like file extensions use grep:

 du -a | grep "\.scala$" | awk -F' '  '{ print $2 }' | xargs sed -i -e 's/subdomainA\.example\.com/subdomainB.example.com/g'
查看更多
看风景的人
4楼-- · 2018-12-31 15:14
cd /home/www && find . -type f -print0 |
  xargs -0 perl -i.bak -pe 's/subdomainA\.example\.com/subdomainB.example.com/g'
查看更多
高级女魔头
5楼-- · 2018-12-31 15:14

For me the easiest solution to remember is https://stackoverflow.com/a/2113224/565525, i.e.:

sed -i '' -e 's/subdomainA/subdomainB/g' $(find /home/www/ -type f)

NOTE: -i '' solves OSX problem sed: 1: "...": invalid command code .

NOTE: If there are too many files to process you'll get Argument list too long. The workaround - use find -exec or xargs solution described above.

查看更多
有味是清欢
6楼-- · 2018-12-31 15:15

Try this:

sed -i 's/subdomainA/subdomainB/g' `grep -ril 'subdomainA' *`
查看更多
闭嘴吧你
7楼-- · 2018-12-31 15:15

Using combination of grep and sed

for pp in $(grep -Rl looking_for_string)
do
    sed -i 's/looking_for_string/something_other/g' "${pp}"
done
查看更多
登录 后发表回答