[Unix] Search for a particular string in files of all sub-directories

[How To] Search for a particular string in files of all sub-directories

There may arise a requirement when you desire to find a file containing a particluar string. However, the problem is that you don't remember where exactly you have placed (mis-placed) the file in dense sub-directory structure. Unix comes to your rescue, unless you have the patience to manually 'cat' each and every file !

Continuing with our previous post, we try to find out a way to search for a particluar string in all files of the current directory, along with sub-directories as well.
 
Below are some of the commands which come handy in such situations. Suppose you wish to search for all the filesnames containing the string "2012".

$ grep -l “*2012*” `find .`-- lists only filenames where the pattern "2012" is found.
$ grep -H “*2012*” `find .`-- lists filenames along with the line in which the pattern "2012" is found.
$ grep "2012" *-- to scan only current directory and not the subdirectories.