搜索并替换linux目录下所有文件中的文本

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/4090194/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-04 23:52:33  来源:igfitidea点击:

Search and replace text in all files of a linux directory

linuxbashtextsedgrep

提问by Yarin

I have a website directory where I need to change all hardcoded links from one domain to another. Looking for a single (grep? sed?) bash command that will allow me to change all occurrences of text in all files in the directory?

我有一个网站目录,我需要将所有硬编码链接从一个域更改为另一个域。正在寻找一个(grep?sed?)bash 命令,它允许我更改目录中所有文件中出现的所有文本?

采纳答案by Michael Goldshteyn

The following will do it:

以下将做到这一点:

sed -i 's/old_link/new_link/g' file...

Don't forget to escape any slashes, dots, and any other regex special chars in the link addresses with a backslash.

不要忘记使用反斜杠对链接地址中的任何斜杠、点和任何其他正则表达式特殊字符进行转义。

回答by Daniel Pereira

Also, try:

另外,请尝试:

perl -p -i -e <regex> <folder>