One of the more annoying things about coding is finding the right file. Or, even worse, finding a file you didn’t know you needed to look at. Especially when the number of files you’re parsing is in the thousands. If you’re on Windows the built-in search can help, but you never know if all the files in the target directory have been indexed.
Luckily, one of the nice things about coding is that you’re often dealing with plain text files. And you are typically searching for a particular string. As they say, there’s an app for that. Both *nix and Windows are capable of searching through the contents an entire hierarchy of files using command-line programs. Each OS has a variety of commands that can do the job, but I’ll highlight the two I use most often here.
In Windows you would use findstr.exe
, and it’s as simple as running the following from the containing directory:
findstr.exe /MIS "searchtext" filetype
On *nix systems the grep
command is your friend:
grep -lr "searchtext" filetype
Of course, a quick google search will get you all the help you need in refining your search.
Update 2012-08-22: What file wasn’t that in?
I recently had a need to find files that did not contain a specific string. A bit more difficult of a prospect. I suspect there’s probably a way to do this on the command line, but so far I’ve only come up with a work-around (in Windows). Use findstr.exe in a loop to show how many times each file contains the string, then filter for files with zero instances:
for /R %f in (filetypes) do find /C /I "searchtext" %f >> ..find.txt
(filetypes
in this instance can be a space-separated list of possible file types.)