Super User is a question and answer site for computer enthusiasts and power users. It's 100% free, no registration required.

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

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

Command above doesn't print the file path.

I'm using: 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 Rabiee Jul 2 '13 at 10:01
    
You may simply want to use ack to grep through all files under the current directory, possibly filtering on certain file types only. – eldering Oct 28 '15 at 20:01
up vote 31 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
    
How would I combine the above commands with search & replace? As in, grep then sed? – CMCDragonkai May 21 '15 at 14:27
1  
the find|grep pipeline outputs filenames, so: sed '...' $(find ... | xargs grep -l ...) – glenn jackman May 21 '15 at 15:31

I use

grep "some string" . -R

and it working faster

share|improve this answer
10  
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
    
This should be marked as the correct answer, to be honest. The currently selected one details something else entirely. – Alhadis Dec 31 '14 at 11:21

For this purpouse I enter in the folder where the files and folders I'm looking for are located and I use:

grep -rinHo "my string"

Doing so I obtain the file name (and path) and the line number that contains the string.

share|improve this answer

I prefered

| find the file |make grep ont finding file | show de full path of the file

find / -type f -exec grep 'some string' {} \; -exec echo {} \;

share|improve this answer

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.