| 场景 | 跨多文件的字符串替换 |
|---|---|
| 组合 | grep -rl + xargs + sed -i |
建议流程
建议流程
grep -rl 'old_domain.com' /www/wwwroot/site --include='*.html' | tee /tmp/hit.txt
grep -c 'old_domain.com' $(cat /tmp/hit.txt) | head # 先看每文件命中次数
xargs -a /tmp/hit.txt sed -i.bak 's/old_domain\.com/new_domain.com/g'
grep -rl 'old_domain.com' /www/wwwroot/site --include='*.html' | wc -l # 复核sed -i.bak 会为每个文件留 .bak:替换确认无误后再统一清理(find ... -name '*.bak' -delete 前先列清单)。