1223

我正在编写grep某些目录的脚本:

{ 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@domain.com

如何将结果限制为仅扩展.h.cpp

4

12 回答 12

1679

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

grep -inr --include \*.h --include \*.cpp CP_Image ~/path[12345] | mailx -s GREP email@domain.com

那应该做你想做的事。

从下面的 HoldOffHunger 答案中获取解释:

  • grep: 命令

  • -r: 递归

  • -i: 忽略大小写

  • -n:每个输出行前面都有其在文件中的相对行号

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

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

于 2012-09-20T16:35:49.843 回答
383

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

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.

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

于 2016-02-08T22:46:38.147 回答
82
grep -rnw "some thing to grep" --include=*.{module,inc,php,js,css,html,htm} ./
于 2015-07-23T19:38:01.333 回答
57

采用:

find . -name '*.h' -o -name '*.cpp' -exec grep "CP_Image" {} \; -print
于 2012-09-20T16:33:57.310 回答
30

There isn't any -r option on HP and Sun servers, but this way worked for me on my HP server:

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

-i is for case insensitive search of string.

于 2015-12-11T15:14:33.987 回答
14

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

Using GNU find you can use the -regex option to find those files in the tree of directories whose extension is either .h or .cpp:

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

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

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 -o to concatenate options (the name is either ending with .h or with .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 "your pattern" -r --include=*.{cpp,h}
#                      ^^^^^^^^^^^^^^^^^^^
于 2016-08-17T09:24:51.033 回答
12

This answer is good:

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

But it can be updated to:

grep -r -i --include \*.{h,cpp} CP_Image ~/path[12345] | mailx -s GREP email@domain.com

Which can be simpler.

于 2018-01-31T02:37:22.730 回答
10

The easiest way is:

find . -type  f -name '*.extension' 2>/dev/null | xargs grep -i string

Add 2>/dev/null to kill the error output.

To include more file extensions and grep for password throughout the system:

find / -type  f \( -name '*.conf' -o -name "*.log" -o -name "*.bak" \) 2>/dev/null |
xargs grep -i password
于 2017-02-05T17:30:47.077 回答
4

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

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

so

ag -G *.h -G *.cpp CP_Image <path>
于 2017-05-10T13:57:11.520 回答
1

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

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

或按 ( ) 分组

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

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

于 2014-11-10T02:09:07.317 回答
1

Here is a method I normally use to find .c and .h files:

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"
于 2015-03-13T11:17:57.860 回答
0

If you want to filter out extensions from the output of another command e.g. "git":

files=$(git diff --name-only --diff-filter=d origin/master... | grep -E '\.cpp$|\.h$')

for file in $files; do
    echo "$file"
done
于 2020-10-09T20:44:09.433 回答