Sundered Peak

through the mind of kyle tolle

Exclude Directory in Recursive grep

I often use a recursive grep to search a Rails app for specific text. Perhaps a CSS class name. I could list the specific directories, but that’s a pain. I’ll just list which directories to ignore, like log, tmp, and the bundle path.

To specifically exclude a directory:

grep -ri --exclude-dir=.bundle "text here" .

A more general way to exclude files, but here a directory:

grep -ri --exclude="*\.bundle*" "text here" .

The following command ignores all the places that won’t have code I’m looking to change:

grep -ri --exclude-dir=.bundle --exclude-dir=log --exclude-dir=tmp "text here" .

I can see the next step here to be adding an alias or function in the shell to make this easier to use. But I’ll do that some other time.

I used the always-helpful StackOverflow to find these answers!