Super User is a question and answer site for computer enthusiasts and power users. Join them; it only takes a minute:

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

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


  • 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
+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
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

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .