Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
378 views
in Technique[技术] by (71.8m points)

unix - 使用grep --exclude / - include语法不要浏览某些文件(Use grep --exclude/--include syntax to not grep through certain files)

I'm looking for the string foo= in text files in a directory tree.

(我在目录树中的文本文件中寻找字符串foo= 。)

It's on a common Linux machine, I have bash shell:

(它在一台普通的Linux机器上,我有bash shell:)

grep -ircl "foo=" *

In the directories are also many binary files which match "foo=".

(在目录中还有许多匹配“foo =”的二进制文件。)

As these results are not relevant and slow down the search, I want grep to skip searching these files (mostly JPEG and PNG images).

(由于这些结果不相关并且减慢了搜索速度,我希望grep跳过搜索这些文件(主要是JPEG和PNG图像)。)

How would I do that?

(我该怎么办?)

I know there are the --exclude=PATTERN and --include=PATTERN options, but what is the pattern format?

(我知道有--exclude=PATTERN--include=PATTERN选项,但模式格式是什么?)

The man page of grep says:

(grep的手册页说:)

--include=PATTERN     Recurse in directories only searching file matching PATTERN.
--exclude=PATTERN     Recurse in directories skip file matching PATTERN.

Searching on grep include , grep include exclude , grep exclude and variants did not find anything relevant

(搜索grep includegrep include excludegrep exclude和variants没有找到任何相关内容)

If there's a better way of grepping only in certain files, I'm all for it;

(如果有一种更好的方法只在某些文件中进行grepping,我就是全力以赴;)

moving the offending files is not an option.

(移动违规文件不是一种选择。)

I can't search only certain directories (the directory structure is a big mess, with everything everywhere).

(我不能只搜索某些目录(目录结构很乱,随处可见)。)

Also, I can't install anything, so I have to do with common tools (like grep or the suggested find ).

(另外,我无法安装任何东西,所以我必须使用常用工具(如grep或建议的查找 )。)

  ask by Piskvor translate from so

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Use the shell globbing syntax:

(使用shell globbing语法:)

grep pattern -r --include=*.{cpp,h} rootdir

The syntax for --exclude is identical.

(--exclude的语法是相同的。)

Note that the star is escaped with a backslash to prevent it from being expanded by the shell (quoting it, such as --include="*.{cpp,h}" , would work just as well).

(请注意,使用反斜杠转义星号以防止它被shell展开(引用它,例如--include="*.{cpp,h}" ,也可以正常工作)。)

Otherwise, if you had any files in the current working directory that matched the pattern, the command line would expand to something like grep pattern -r --include=foo.cpp --include=bar.h rootdir , which would only search files named foo.cpp and bar.h , which is quite likely not what you wanted.

(否则,如果当前工作目录中的任何文件与模式匹配,命令行将扩展为类似grep pattern -r --include=foo.cpp --include=bar.h rootdir ,它只会搜索文件名为foo.cppbar.h ,很可能不是你想要的。)


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...