在Linux下列出包含指定字符串或正则表达式的文件名

来源:岁月联盟 编辑:exp 时间:2011-11-02

 

命令:grep -l <string_or_regex> <file_pattern>

 

 

 

其中-l参数(小写L)的含义如下:

 

man grep 写道

-l, --files-with-matches

       Suppress  normal output; instead print the name of each input file from which output would normally have been printed.  The scanning will stop on the first match.

 

 

因为遇到第一个匹配之后就会停止文件内容的扫描,所以效率挺高。找到之后再用别的工具去看文件的具体内容。

 

 

 

举例:

 

grep -l SELECT NSI.LOG.2011-09-*

 

grep -l 'NssAddUser.*CRM' NSI.LOG.2011-08-*

 

grep -l 'NssAddUser.*CRM' NSI.LOG.2011-08-2*

 

 

 

[root@db1 logs]# grep -l 'NssAddUser.*CRM' NSI.LOG.2011-08-2*

NSI.LOG.2011-08-22-08

NSI.LOG.2011-08-22-09

NSI.LOG.2011-08-22-10

NSI.LOG.2011-08-23-09

NSI.LOG.2011-08-25-09

NSI.LOG.2011-08-25-10

NSI.LOG.2011-08-26-08

NSI.LOG.2011-08-26-09

[root@db1 logs]#