问题描述
如何查找和替换每次出现的:
How do I find and replace every occurrence of:
subdomainA.example.com
与
subdomainB.example.com
在/home/www/
目录树下的每个文本文件中递归?
in every text file under the /home/www/
directory tree recursively?
推荐答案
find /home/www ( -type d -name .git -prune ) -o -type f -print0 | xargs -0 sed -i 's/subdomainA.example.com/subdomainB.example.com/g'
-print0
告诉 find
打印由空字符分隔的每个结果,而不是换行.万一您的目录包含名称中带有换行符的文件,这仍然可以让 xargs
处理正确的文件名.
-print0
tells find
to print each of the results separated by a null character, rather than a new line. In the unlikely event that your directory has files with newlines in the names, this still lets xargs
work on the correct filenames.
( -type d -name .git -prune )
是一个完全跳过所有名为 .git
的目录的表达式.如果您使用 SVN 或想要保留其他文件夹,您可以轻松扩展它——只需匹配更多名称即可.它大致相当于 -not -path .git
,但效率更高,因为它不会检查目录中的每个文件,而是完全跳过它.后面的 -o
是必需的,因为 -prune
的实际工作方式.
( -type d -name .git -prune )
is an expression which completely skips over all directories named .git
. You could easily expand it, if you use SVN or have other folders you want to preserve -- just match against more names. It's roughly equivalent to -not -path .git
, but more efficient, because rather than checking every file in the directory, it skips it entirely. The -o
after it is required because of how -prune
actually works.
有关详细信息,请参阅man find
.
For more information, see man find
.
这篇关于如何使用 awk 或 sed 递归查找/替换字符串?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!