Exclude files from recursive grep the easy way

Posted on


You’re working on a project with a ton of files, and you need to find that needle in the haystack. No problem, you fallback on your old friend:

grep -Rn 'neeedle' .

The only problem is that your project directory also contains a bunch of build files that you don’t want to include in your recursive grep search. For example, .meteor/ under a MeteorJS project contains all of your code minified and bundled.


Assuming that you’re using git to version your files (if you’re not, you should be) you can use git grep.

git grep 'TODO'

This will recursively search only the files that are tracked in your repository (respects .gitignore).

Note: You can also use --exclude-dir with GNU Grep >=2.5.2 but who wants to type that out?

Leave a Reply

Your email address will not be published.