Linux 如何使用 grep 在当前目录中搜索所有具有字符串“hello”但仅显示 .h 和 .cc 文件的文件?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/9217185/
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
How do I use grep to search the current directory for all files having the a string "hello" yet display only .h and .cc files?
提问by stackoverflow
How do I use grep to search the current directory for any and all files containing the string "hello" and display only .h and .cc files?
如何使用 grep 在当前目录中搜索包含字符串“hello”的所有文件并仅显示 .h 和 .cc 文件?
采纳答案by stackoverflow
grep -r --include=*.{cc,h} "hello" .
This reads: search recursively (in all sub directories also) for all .cc OR .h files that contain "hello" at this .
(current) directory
这读作:递归搜索(也在所有子目录中)在此.
(当前)目录中包含“hello”的所有 .cc 或 .h 文件
回答by Donald Miner
You can pass in wildcards in instead of specifying file names or using stdin.
您可以传入通配符,而不是指定文件名或使用标准输入。
grep hello *.h *.cc
回答by Noufal Ibrahim
find . -name \*.cc -print0 -or -name \*.h -print0 | xargs -0 grep "hello"
.
find . -name \*.cc -print0 -or -name \*.h -print0 | xargs -0 grep "hello"
.
Check the manual pages for find
and xargs
for details.
检查手册页find
,并xargs
了解详细信息。
回答by Jonathan Leffler
If you need a recursive search, you have a variety of options. You should consider ack
.
如果您需要递归搜索,您有多种选择。你应该考虑ack
。
Failing that, if you have GNU find
and xargs
:
否则,如果您有 GNUfind
并且xargs
:
find . -name '*.cc' -print0 -o -name '*.h' -print0 | xargs -0 grep hello /dev/null
The use of /dev/null
ensures you get file names printed; the -print0
and -0
deals with file names containing spaces (newlines, etc).
使用/dev/null
确保您打印文件名;在-print0
和-0
涉及包含空格的文件名(换行等)。
If you don't have obstreperous names (with spaces etc), you can use:
如果您没有令人讨厌的名字(带空格等),您可以使用:
find . -name '*.*[ch]' -print | xargs grep hello /dev/null
This might pick up a few names you didn't intend, because the pattern match is fuzzier (but simpler), but otherwise works. And it works with non-GNU versions of find
and xargs
.
这可能会选择一些您不想要的名称,因为模式匹配更模糊(但更简单),但除此之外还可以。它的工作原理与非GNU版本find
和xargs
。
回答by glenn Hymanman
grep -l hello **/*.{h,cc}
You might want to shopt -s nullglob
to avoid error messages if there are no .h or no .cc files.
shopt -s nullglob
如果没有 .h 或 .cc 文件,您可能希望避免出现错误消息。
回答by Hyman
If I read your question carefully, you ask to "grep to search the current directory for any and all filescontaining the string "hello" and display only .h and .cc files". So to meet your precise requirements here is my submission:
如果我仔细阅读了您的问题,您会要求“grep 搜索当前目录中包含字符串“hello”的任何和所有文件,并仅显示 .h 和 .cc 文件”。因此,为了满足您的确切要求,这是我的提交:
This displays the file names:
这将显示文件名:
grep -lR hello * | egrep '(cc|h)$'
...and this display the file names and contents:
...这将显示文件名和内容:
grep hello `grep -lR hello * | egrep '(cc|h)$'`
回答by user1733951
The simplest way : grep -Ril "Your text" /
最简单的方法:grep -Ril "Your text" /
回答by AB Abhi
To search in current directory recursively:
在当前目录中递归搜索:
grep -r 'myString' .