如何对多种文件类型使用 grep --include 选项? [英] how do I use the grep --include option for multiple file types?

查看:39
本文介绍了如何对多种文件类型使用 grep --include 选项?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当我想 grep 某个目录中的所有 html 文件时,我执行以下操作

When I want to grep all the html files in some directory, I do the following

grep --include="*.html" 模式 -R/some/path

效果很好.问题是如何grep某个目录下的所有html,htm,php文件?

which works well. The problem is how to grep all the html,htm,php files in some directory?

从此使用grep --exclude/--include 语法不通过某些文件进行 grep ,看来我可以执行以下操作

From this Use grep --exclude/--include syntax to not grep through certain files, it seems that I can do the following

grep --include="*.{html,php,htm}" 模式 -R/some/path

但遗憾的是,它对我不起作用.
仅供参考,我的 grep 版本是 2.5.1.

But sadly, it would not work for me.
FYI, my grep version is 2.5.1.

推荐答案

您可以使用多个 --include 标志.这对我有用:

You can use multiple --include flags. This works for me:

grep -r --include=*.html --include=*.php --include=*.htm "pattern" /some/path/

但是,您可以按照Deruijter 的建议进行操作.这对我有用:

However, you can do as Deruijter suggested. This works for me:

grep -r --include=*.{html,php,htm} "pattern" /some/path/

不要忘记你也可以使用 findxargs 来处理这类事情:

Don't forget that you can use find and xargs for this sort of thing too:

find /some/path/ -name "*.htm*" -or -name "*.php" | xargs grep "pattern"

这篇关于如何对多种文件类型使用 grep --include 选项?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆