I would like to know which files contain a certain string. If I use 'grep "string" *' only the working directory is being searched. I also want to search the subdirectories. When I use 'find . -type f -print |xargs grep "string" > dev/null' I get the message 'xargs: missing quote?'. What's up?
Should I use another command?
However, this will only find you the filenames that contain the string. It will not search for the string <I>within</I> the file, which is what the original poster was asking about.
Doing a find and then greping will certainly find the string within a string (in the file name) and works much better and faster. You are right PxT, this will not search the string within the file.
find / | grep lib
You will find a zillion occurances of the string 'lib' within a string. Here is a small sample and I've not even refined the grep or egrep (and just captured small output).
Yes, it does not search the actual file. I normally use PERL for that and not xargs. For some strange reason, xargs and I don't gel.... when I have to search files and strings within lots of files I use command line PERL.
grep -r "string" * works pretty well on Linux(RH).
However this is not a comprehensive method. But I think this could be used for what poster is mentioned since he is using '*'(this cause grep read all files under each directory recursively).
But if he uses grep -r "www" html* will only recurse into subdirectories whose names starts with "html". so if there is a subdirectory whose name is not starts with "html" will be ignored even if it has the file that contain "www" string.
I haven't used this in a while, but I recall getting errors because this usage of find also 'finds' directories, binaries, and just about everything else under the sun. Because it finds binaries, directories, etc. it attempts to search them and gets pretty messy.
find . -type r -exec grep string {} /dev/null \;
Will find only regular files, and not directories, but does not stop the grep from searching large binaries, as I recall. That is why I usually don't use this combo for grepping strings and forgot about this one. I don't recall a switch in find to stop it from 'finding' binary files. Perhaps there is ?