grep搜索递归不起作用

时间:2017-01-31 04:36:15

标签: bash grep

我试图在所有日志文件中找到一个字符串并列出这些文件名。

这些日志文件位于子目录中,这些文件使用gz压缩,因此使用zgrep。

我正在使用以下命令

zgrep -lFR --include=Std_20170101*  "ReceiveData: Receive call failed" /logs/nas/App02/201612* >> zfileslist

我正在使用--include = Std_20170101 *是App02位置后所有子目录中的文件名。所以使用上面的上下文来节省时间。

我确信我们在这些日期日志文件中有条目,但有些条目没有显示它们。

请帮忙。

2 个答案:

答案 0 :(得分:1)

使用--include-dir选项,因为--include仅限于文件:

zgrep -lFR --include-dir="Std_20170101*" "ReceiveData: Receive call failed" /logs/nas/App02/201612* >> zfileslist

来自grep手册页:

**--include**
  If specified, only files matching the given filename pattern are searched.
  Note that --exclude patterns take priority over --include patterns.  
  Patterns are matched to the full path specified, not only to the filename component.

**--include-dir**
  If -R is specified, only directories matching the given filename pattern are searched.
  Note that --exclude-dir patterns take priority over --include-dir patterns.

答案 1 :(得分:0)

如果你想提供模式,那么使用find with -exec选项并对返回的文件运行grep。

find ./ -name "*.dat" -exec grep "find pattern" {} \;

相关问题