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.

I am trying to find a particular string of text in a file somewhere on my hard drive. The best I have come up with so far is:

find -type f / | xargs cat | grep "string to find"

But this just tells me whether the string exists somewhere or not.

How can I know what file I have found it in?

Is there a better way?

share|improve this question

6 Answers 6

up vote 4 down vote accepted
find / -type f -exec grep "string" {} +

This will perform a case insensitive search which will be slower, but more flexible:

find / -type f -exec grep -i "string" {} +

you may get a lot of results so you can pipe it to less, then use f and b to navigate forward and backward page by page through the output:

find / -type f -exec grep -i "string" {} + | less

If you know you are looking for *.txt files, you can refine it even more:

find / -type f -name "*.txt" -exec grep -i "string" {} + | less
share|improve this answer

This should do the trick and is a lot simpler:

grep -R "some string" /

adding -n will show you the line numbers you need that:

grep -Rn "some string" /
share|improve this answer

Why use 'cat' at all in this case? I always find it odd how people use 'cat' for no reason at all. Just do:

find -type f / | xargs grep "string to find"

Of course searching every single file on your entire disk will be quite slow. You're probably better off using some kind of indexing program if you find yourself needing to search like this often. For example Beagle is a quite extensive search engine, it's been around for a while. Other options are KDE's Strigi or Google's Desktop Search

At the very least, you should use 'locate' instead of 'find', it only indexes file names and not their contents, but that will still speed things up. In addition you should filter based on file type before using grep. Eg using grep on a 100MB avi file or something is just a waste of time.

share|improve this answer

what about:

grep -r -H -i 'string' .
share|improve this answer

How can I know what file I have found it in?

You need to use the '-H' option with 'grep' for printing the matching file.


Also,

  • It would also be a good idea to use a slightly better guess than the root ('/').
  • John's scheme of the -exec switch over the '|' operator will help when you have a lot of files being searched
share|improve this answer
3  
+1 - You should almost never need to start a search at /. It's also worth saying that if you run a search starting from / as a regular user, you're going to end up with a ton of error messages because of permissions problems. –  Telemachus Nov 6 '09 at 22:01
2  
grep -H wouldn't work in this case due to his redundant use of 'cat'. it would just print 'stdin'. Now if he didn't use 'cat', then the default setting of grep is to print which file it matches in, so he wouldn't need the extra flag. –  davr Nov 6 '09 at 23:50

Yep, grep with the -r flag will search recursively. Try a command like:

grep -ri 'hello' .

Which will search the current directory, and every file and directory inside it, for the phrase 'hello'. The -i flag makes it case-independent, so it'll also find "Hello", "HELLO", etc.

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.