I find myself needing this more and more often lately, so I figured this is as good a place as any to keep track of it. Here's how to recursively search for files containing a particular string or regular expression. This should work on most Linux or UNIX systems, depending on your configuration.
find -type f -name "*" | xargs grep 'word, phrase, or regular expression'
Summary for the curious:
- find returns the names of one or more files, but not their contents.
- -type f tells find to only look at files, not directories.
- | "pipes" the output of the command on the left to the input of the command on the right.
- xargs creates and executes a command based on the arguments you pass to it.
- grep searches the contents of files and returns each line that is a match.
- '...' is a regular expression (or just a word, if you prefer) that indicates what you are searching for.