Linux grep,但只有某些文件扩展名

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/12516937/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-06 14:24:35  来源:igfitidea点击:

grep, but only certain file extensions

linuxunixsearchcommand-linegrep

提问by Jason

I am working on writing some scripts to grepcertain directories, but these directories contain all sorts of file types.

我正在将一些脚本写入grep某些目录,但这些目录包含各种文件类型。

I want to grepjust .hand .cppfor now, but maybe a few others in the future.

我想grep只是.h.cpp现在,但也许其他几个人的未来。

So far I have:

到目前为止,我有:

{ grep -r -i CP_Image ~/path1/;

grep -r -i CP_Image ~/path2/;

grep -r -i CP_Image ~/path3/;

grep -r -i CP_Image ~/path4/;

grep -r -i CP_Image ~/path5/;} 

| mailx -s GREP [email protected]

Can anyone show me how I would now add just the specific file extensions?

谁能告诉我我现在如何只添加特定的文件扩展名?

采纳答案by Nelson

Just use the --includeparameter, like this:

只需使用--include参数,如下所示:

grep -r -i --include \*.h --include \*.cpp CP_Image ~/path[12345] | mailx -s GREP [email protected]

that should do what you want.

那应该做你想做的。

To take the explanation from HoldOffHunger's answerbelow:

从以下HoldOffHunger 的回答中获取解释:

  • grep: command

  • -r: recursively

  • -i: ignore-case

  • --include \*.cpp: all *.cpp: C++ files (escape with \ just in case you have a directory with asterisks in the filenames)

  • ./: Start at current directory.

  • grep: 命令

  • -r: 递归

  • -i: 忽略大小写

  • --include \*.cpp: 所有 *.cpp: C++ 文件(用 \ 转义,以防您的目录中文件名带有星号)

  • ./: 从当前目录开始。

回答by Amir Afghani

How about:

怎么样:

find . -name '*.h' -o -name '*.cpp' -exec grep "CP_Image" {} \; -print

回答by where23

Should write "-exec grep " for each "-o -name "

应该为每个“-o -name”写“-exec grep”

find . -name '*.h' -exec grep -Hn "CP_Image" {} \; -o -name '*.cpp' -exec grep -Hn "CP_Image" {} \;

Or group them by ( )

或按 ( )

find . \( -name '*.h' -o -name '*.cpp' \) -exec grep -Hn "CP_Image" {} \;

option '-Hn' show the file name and line.

选项“-Hn”显示文件名和行。

回答by Tom

I am aware this question is a bit dated, but I would like to share the method I normally use to find .cand .hfiles:

我知道这个问题有点过时了,但我想分享我通常用来查找.c.h文件的方法:

tree -if | grep \.[ch]\b | xargs -n 1 grep -H "#include"

or if you need the line number as well:

或者如果您还需要行号:

tree -if | grep \.[ch]\b | xargs -n 1 grep -nH "#include"

回答by Yuri Malov

grep -rnw "some thing to grep" --include=*.{module,inc,php,js,css,html,htm} ./

回答by Sam B

There is no -r option on HP and Sun servers, this way worked for me on my HP server

HP 和 Sun 服务器上没有 -r 选项,这种方式在我的 HP 服务器上对我有用

find . -name "*.c" | xargs grep -i "my great text"

-i is for case insensitive search of string

-i 用于不区分大小写的字符串搜索

回答by HoldOffHunger

Some of these answers seemed too syntax-heavy, or they produced issues on my Debian Server. This worked perfectly for me:

其中一些答案似乎语法过于繁重,或者它们在我的 Debian 服务器上产生了问题。这对我来说非常有效:

PHP Revolution: How to Grep files in Linux, but only certain file extensions?

PHP 革命:如何在 Linux 中 Grep 文件,但只有某些文件扩展名?

Namely:

即:

grep -r --include=\*.txt 'searchterm' ./

...or case-insensitive version...

...或不区分大小写的版本...

grep -r -i --include=\*.txt 'searchterm' ./
  • grep: command

  • -r: recursively

  • -i: ignore-case

  • --include: all *.txt: text files (escape with \ just in case you have a directory with asterisks in the filenames)

  • 'searchterm': What to search

  • ./: Start at current directory.

  • grep: 命令

  • -r: 递归

  • -i: 忽略大小写

  • --include: 所有 *.txt: 文本文件(用 \ 转义以防万一您的目录中文件名带有星号)

  • 'searchterm': 搜索什么

  • ./: 从当前目录开始。

回答by fedorqui 'SO stop harming'

Since this is a matter of findingfiles, let's use find!

由于这是查找文件的问题,让我们使用find!

Using GNU find you can use the -regexoption to find those files in the tree of directories whose extension is either .hor .cpp:

使用 GNU find 您可以使用该-regex选项在目录树中查找扩展名为.h或 的文件.cpp

find -type f -regex ".*\.\(h\|cpp\)"
#            ^^^^^^^^^^^^^^^^^^^^^^^

Then, it is just a matter of executing grepon each of its results:

然后,只需执行grep其每个结果:

find -type f -regex ".*\.\(h\|cpp\)" -exec grep "your pattern" {} +

If you don't have this distribution of find you have to use an approach like Amir Afghani's, using -oto concatenate options (the name is either ending with .hor with .cpp):

如果您没有 find 的这种分布,则必须使用类似Amir Afghani 的方法-o用于连接选项(名称以.h或结尾.cpp):

find -type f \( -name '*.h' -o -name '*.cpp' \) -exec grep "your pattern" {} +
#            ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^


And if you really want to use grep, follow the syntax indicated to --include:

如果您真的想使用grep,请遵循指示的语法--include

grep "your pattern" -r --include=*.{cpp,h}
#                      ^^^^^^^^^^^^^^^^^^^

回答by user3640130

The easiest way is

最简单的方法是

find . -type  f -name '*.extension' | xargs grep -i string 

回答by Eldamir

ag(the silver searcher) has pretty simple syntax for this

ag(银搜索器)对此有非常简单的语法

       -G --file-search-regex PATTERN
          Only search files whose names match PATTERN.

so

所以

ag -G *.h -G *.cpp CP_Image <path>