bash tip: find and grep through files

It happens once in a while that I want to find all files containing a certain string. I know command-line tools such as grep, cat, and find, but I never remember the right combination to achieve that task. So as to remember and reference it later, I write this small blog post to remind me how to do it:
find . -type f -exec grep YOURSTRING /dev/null {} \;
The find . part will search from the current directory (and all its subdirectories), - type f to search for files (not directories or links, etc), -exec to use the grep command to find through the files, with YOURSTRING as query string, /dev/null to throw away the errors you don't care about, and {} is the current file to search into with grep.

Update: a simple approach proposed in the comments is
grep -r YOURSTRING *


© 2012 Guillaume Laforge | The views and opinions expressed here are mine and don't reflect the ones from my employer.