search files that contain a specific string
November 9th, 2008
Many times you find yourself at the top of complex directory tree, seeking for a file that contains some specific string value. Nautilus is not very helpful now, as it can only search on file names, not content. It seems that the command line is the only way.
Of course, you can always visit every single subdirectory in the tree and grep all files each time. But this is very inefficient and time consuming as the number of subdirectories grow larger, not to mention the probability of skipping some subdirectories by mistake.
"There must be a better way", you may think. And you are right. This is the command that will do the job for you:
This command will output all the files under /path/to/top/level/directory that contain the stringl [neede]. The ; at the end is necessary to denote the end of the command. Make sure that you use appropriate escaping (\) according to your shell's fads.
If you are a Windows user and you cannot search within your files' contents (that's ok, it's not your fault, it's just that bloody windows explorer that never functioned...), you can also use the solution described here, at the cost of installing GNU utilities for Win32.
Of course, you can always visit every single subdirectory in the tree and grep all files each time. But this is very inefficient and time consuming as the number of subdirectories grow larger, not to mention the probability of skipping some subdirectories by mistake.
"There must be a better way", you may think. And you are right. This is the command that will do the job for you:
find /path/to/top/level/directory -type f -exec grep -F -H [needle] {} \;This command will output all the files under /path/to/top/level/directory that contain the stringl [neede]. The ; at the end is necessary to denote the end of the command. Make sure that you use appropriate escaping (\) according to your shell's fads.
If you are a Windows user and you cannot search within your files' contents (that's ok, it's not your fault, it's just that bloody windows explorer that never functioned...), you can also use the solution described here, at the cost of installing GNU utilities for Win32.
March 9th, 2009 at 1:28 pm I have written that exact command many times in the past and there are a couple of things I would add to it.
If you have a GNU version of grep then you can replicate that functionality with "grep -r" which searches recursively down from the directory you are currently in. (I used do a lot of work on Solaris boxes which didn't necessarily have the GNU version of grep)
However, you can do more with the "find" version such as limiting which types of files you wish to search in. For instance, a web root directory is likely to have lots of PHP files in it along with image files, mp3 files, zip/gz files and probably .svn directories with duplicates of all the above files. You can extend the "find" command to only find files that end in ".php" which will significantly reduce the amount of time it spends looking.
As far as Windows goes, I install Cygwin almost as soon as I get a new Windows box. I've spent so long working with Unix that the solution I first think of is the way you would do it on Unix. It's nice not to have to remember the Windows way as well.