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.

How can I locate all files in directory /home/user/example that contain "some text" in Linux. I am using Ubuntu 9.10.

share|improve this question

3 Answers 3

up vote 6 down vote accepted

I usually use a combination of find and grep so it grabs all hidden files as well:

find /home/user/example -type f -exec grep -i "some text" {} +

The -i switch to grep will use a case-insensitive search method. It will be slower, but it is useful if you don't know the exact case of the words. You can remove the switch if you know what the proper case is to speed things up.

share|improve this answer
    
I believe you need to add the -H parameter to grep; otherwise the file's name will not be shown, only the line of text that matches will. –  eduffy Jan 4 '10 at 20:15
    
It shows both for me –  John T Jan 4 '10 at 20:23
    
It must be the + sign at the end. Does that ensure that grep is only executed once will all results? I usually end the exec with a ; which would execute grep once per result. –  eduffy Jan 5 '10 at 2:26
1  
You're correct. The + will not fork like the \; will. –  John T Jan 5 '10 at 2:52
grep -r "some text" /home/user/example

Will recurse through all sub folders looking for the string.

share|improve this answer

If you're not worried about recursing into subdirectories,

grep -l "some text" *

Will only output the filenames, and not the context that the match appears in.

share|improve this answer
    
what about hidden files? –  John T Jan 4 '10 at 19:52
    
good point; my solution is far more limited than yours is. On the other hand, hiding an example file would be a fairly evil trick :P –  Babu Jan 5 '10 at 0:06

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.