How to grep a word inside xml files in a folder

送分小仙女□ 提交于 2019-12-22 01:39:50

问题


I know I can use grep to find a word in all the files present in a folder like this

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 ?

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.


回答1:


Try the --include option

grep -R --include="*.xml" "pattern" /path/to/dir

Reference: Grep Include Only *.txt File Pattern When Running Recursive Mode




回答2:


Use find:

find /path/to/dir -name '*.xml' -exec grep -H 'pattern' {} \;



回答3:


I use this often:

grep 'pattern' /path/to/dir/**/*.xml

EDIT:

with zsh



来源:https://stackoverflow.com/questions/21428078/how-to-grep-a-word-inside-xml-files-in-a-folder

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!