totalitarian
totalitarian

Reputation: 3676

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?

Upvotes: 1

Views: 5298

Answers (1)

fedorqui
fedorqui

Reputation: 290315

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.)

Upvotes: 4

Related Questions