Using grep with the –exclude-dir flag to exclude multiple directories

grepregular expression

I am searching through a Ruby on Rails application for a word using grep on OSX, and I would like to exclude directories that match a certain pattern.

I am using the following command:

grep -inRw -E 'direct' . --exclude-dir -E 'git|log|asset'

This command is not doing what I thought it would do. Here is how I thought it would work:

  • i – case insensitive search
  • n – print line number in which pattern is found
  • R – search recursively
  • w – I only want whole words – i.e., match "direct" but not "directory"
  • -E – use extended regular expression
  • 'direct' – the regular expression I want to match
  • . – search in the current directory
  • –exclude-dir -E 'git|log|asset' – exclude directories that match git or log or asset.

In terms of the exclude directories, the command still ends up searching in the './git' and './log' directories, as well as in './app/assets'

I'm obviously lacking a fundamental piece of knowledge, but I do not know what it is.

Best Answer

The man page description for that option is sort of misleading... It's pattern as in globs not pattern as in regex. Per the info page:

--exclude-dir=GLOB

Skip any command-line directory with a name suffix that matches the pattern GLOB. When searching recursively, skip any subdirectory whose base name matches GLOB. Ignore any redundant trailing slashes in GLOB.

So, in your case you could run:

grep -inRw -E 'direct' . --exclude-dir={git,log,assets}

to exclude directories named git, log and assets or e.g.

grep -inRw -E 'direct' . --exclude-dir={\*git,asset\*}

to exclude directory names ending in git or starting with asset.

Related Question