How to find all files containing a string?

I'm using this

# cat *.php* | grep -HRi error_reporting

This is my result

(standard input):$mosConfig_error_reporting = '0';
(standard input):error_reporting(E_ALL);

How can I find out what files contain the results?


Use -l option to show the file name only:

grep -il "error_reporting" *php*

For the recursion, you can play with --include to indicate the files you want to look for:

grep -iRl --include=*php* "error_reporting" *

But if you want to show the line numbers, then you need to use -n and hence -l won't work alone. This is a workaround:

grep -iRn --include="*php*" "error_reporting" * | cut -d: -f-2

or

find . -type f -name "*php*" -exec grep -iHn "error_reporting" {} ; | cut -d: -f-2. 

The cut part removes the matching text, so that the output is like:

file1:line_of_matching
file2:line_of_matching
...

From 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. (-l is specified by POSIX.)

--include=GLOB

Search only files whose base name matches GLOB (using wildcard matching as described under --exclude).

-n , --line-number

Prefix each line of output with the 1-based line number within its input file. (-n is specified by POSIX.)

链接地址: http://www.djcxy.com/p/13678.html

上一篇: Unix:在位于不同路径的文件中搜索单词

下一篇: 如何查找包含字符串的所有文件?