关于linux:grep没有显示路径/文件:行

grep without showing path/file:line

你是如何grep并且只返回匹配行的?即,结果中省略了路径/文件名。

在这种情况下,我希望查找当前目录中的所有.bar文件,搜索术语foo

1
find . -name '*.bar' -exec grep -Hn FOO {} \;

无需find。如果您只是在特定目录中查找模式,那么这就足够了:

1
grep -hn FOO /your/path/*.bar

其中,-h是隐藏文件名的参数,如从man grep中:

-h, --no-filename

Suppress the prefixing of file names on output. This is the default
when there is only one file (or only standard input) to search.

请注意,您正在使用

-H, --with-filename

Print the file name for each match. This is the default when there is
more than one file to search.


-h代替-h。有关选项的更多详细信息,请查看man grep

1
find . -name '*.bar' -exec grep -hn FOO {} \;


从手册页:

1
2
3
-h, --no-filename
    Suppress the prefixing of file names on output. This is the default when there
    is only one file (or only standard input) to search.