Linux 查找和替换
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/8588789/
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
Linux find and replace
提问by Subhojit Mukherjee
How can I replace "abc"
with "abcd"
on all files of a folder using shell?
我怎么能代替"abc"
与"abcd"
上使用shell文件夹中的所有文件?
Is it possible using sed
command?
可以使用sed
命令吗?
采纳答案by kbdjockey
Try the following command for the file file.txt:
对文件 file.txt 尝试以下命令:
sed -i 's/abc/abcd/g' file.txt
Try the following command for all files in the current folder:
对当前文件夹中的所有文件尝试以下命令:
find . -maxdepth 1 -type f -exec sed -i 's/abc/abcd/g' {} \;
For the files in the current directory and all subdirectories:
对于当前目录和所有子目录中的文件:
find . -type f -exec sed -i 's/abc/abcd/g' {} \;
Or if you are fan of xargs:
或者,如果您是 xargs 的粉丝:
find . -type f | xargs -I {} sed -i 's/abc/abcd/g' {}
回答by Nikodemus RIP
sed -i 's/abc/&d/g' *
should work.
应该管用。
回答by fge
Yes:
是的:
find /the/folder -type f -exec sed -i 's,\<abc\>,&d,g' {} \;