Last Updated: January 03, 2018
·
2.765K
· ebradbury

Exclude files from recursive grep the easy way

Scenario

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.

Solution

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?