如何替换 Mac 文件夹中的每个文本?

Hoa*_*ham 1 find-and-replace macos

在 Mac OS 中,如何查找和替换所有目录及其子目录中的特定文本?

Ada*_*eld 5

使用的组合find(1)sed(1)

# Find all files under the directory hierarchy rooted at 'root', and replace
# all instances of the regular expression 'pattern' with 'replacement' in all
# of those files:
find root -type f -exec sed -i~ 's/pattern/replacement/g' '{}' '+'
Run Code Online (Sandbox Code Playgroud)

如果遇到命令行长度限制,请将'+'末尾的替换为';'. 这将使它运行得更慢(因为它要为每个文件派生一个新进程),但它不会有命令行太长的危险sed

您也可以通过添加适当的过滤器来仅替换某些文件find(例如-name *.txt仅替换 .txt 文件)。