13

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.

12

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.

  • Very interesting. I guess I should look up what a glob is. – Steven L. May 12 '16 at 0:24
  • I'm sorry but none of these seem to be working. Would it help if I if I said that I'm using this version of grep: grep (BSD grep) 2.5.1-FreeBSD? – Steven L. May 12 '16 at 1:10
  • I'm stumped because I'm reading the manual for bsd grep (freebsd.org/cgi/man.cgi?query=grep&sektion=&n=1) and there's not mention of --exclude-dir. Boo. – Steven L. May 12 '16 at 2:21
  • 2
    OK - I see the problem now. This must be a modified version of grep used by the Apple system only. I am on Mac OS X. I'm sorry for the confusion here. I'm sure there's a forum somewhere else on stackexchange I can post to. – Steven L. May 12 '16 at 2:23
  • @user245185 are you on BSD or on OSX? Both are on topic here, but please edit your question and tell us what OS you are using. The behaviors of the standard tools differ between implementations as you're discovering. – terdon May 12 '16 at 8:49
7

--exclude-dir does not understand |.

You can, however, get the same effect by specifying --exclude-dir multiple times, one for each directory that you want to exclude:

grep -inRw -E --exclude-dir 'asset' --exclude-dir 'git' --exclude-dir 'log' 'direct'
  • It worked for me. I wanted to exclude home directory but it didn't work with other options like --exclude-dir={home}. I worked when I typed --exclude-dir 'home' – Mubin Icyer Aug 14 at 10:05

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.