Linux 如何grep文件夹中的xml文件中的单词
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/21428078/
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
How to grep a word inside xml files in a folder
提问by sachinjain024
I know I can use grep to find a word in all the files present in a folder like this
我知道我可以使用 grep 在这样的文件夹中存在的所有文件中查找单词
grep -rn core .
But my current directory has many sub-directories and I just want to search in all xml files present in the current directory and its all sub directories. How can I do that ?
但是我的当前目录有很多子目录,我只想搜索当前目录及其所有子目录中存在的所有 xml 文件。我怎样才能做到这一点 ?
I tried this
我试过这个
grep -rn core *.xml // Does not work
But it searches for xml files present in the current directory only. It does not do it recursively.
但它只搜索当前目录中存在的 xml 文件。它不会递归执行。
采纳答案by Chris Maes
Try the --include option
尝试 --include 选项
grep -R --include="*.xml" "pattern" /path/to/dir
Reference: Grep Include Only *.txt File Pattern When Running Recursive Mode
回答by Kent
I use this often:
我经常使用这个:
grep 'pattern' /path/to/dir/**/*.xml
EDIT:
编辑:
with zsh
与 zsh
回答by choroba
Use find
:
使用find
:
find /path/to/dir -name '*.xml' -exec grep -H 'pattern' {} \;