Take the 2-minute tour ×
Super User is a question and answer site for computer enthusiasts and power users. It's 100% free, no registration required.

find . -type f -print -exec cat {} \; | grep some string

is not printing the file path in the above statement.

Ubuntu, bash 4

share|improve this question
    
Maybe it does not meet your expected results because of the grep filter. What about this: find . -type f -fprint /dev/stderr -exec cat {} \; | grep some string –  artistoex Jul 2 '13 at 9:44
    
What is the goal you want to achieve? On output you are getting grepped strings from files, you want path of those files? –  mirkobrankovic Jul 2 '13 at 9:58
    
Yes I want to print the path of the files that contain the string 'some string'. @artistoex suggestion prints all file paths. –  hajder Jul 2 '13 at 10:01

2 Answers 2

up vote 3 down vote accepted

That's because you're feeding grep a stream of text which just happens to contain filenames. Since you provided no filenames as arguments to grep, it cannot be expected to deduce what file a matched line came from. Use xargs:

find . -type f -print | xargs grep "some string"

Since you have GNU find/xargs, this is a safer way for xargs to read filenames:

find . -type f -print0 | xargs -0 grep "some string"

If you only want the filenames that have a matching line without showing the matching line:

find . -type f -print0 | xargs -0 grep -l "some string"
share|improve this answer

I use

grep "some string" . -R

and it working faster

share|improve this answer
1  
And -n if you want line numbers. +1 for simplified answer. –  Chad Skeeters Jul 3 '13 at 16:49
    
@ChadSkeeters exactly. Using the find does not give such solution. –  ArcherGodson Jul 9 '13 at 12:45

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.