I am trying to find a file that contains the text "The default interactive shell". I am trying to use the following grep command to find it:

grep -r --exclude-dir='/{var,Volumes,bin,cores,dev,sbin,tmp,usr}' "The default interactive shell" /

however it still searches /usr as shown by this error message:

grep: /usr/bin/sudo: Permission denied

How can I perform a recursive grep and exclude multiple directories without writing multiple --exclude-dir options? The system is OSX 10.15.4.

  • 4
    Try removing the single-quotes around the list of directories. Without them, the shell will expand the brace expression to --exclude-dir=/var --exclude-dir=/Volumes --exclude-dir=/bin ... etc. – Gordon Davisson Apr 19 at 18:22
  • Thanks, that worked! – user109923 Apr 20 at 4:27

You are excluding a directory with the literal name /{var,Volumes,bin,cores,dev,sbin,tmp,usr}. This is due to the single quotes. When a brace expansion is quoted, it is no longer a brace expansion, just ordinary text.

To properly let the brace expansion expand, remove the quotes:

grep -R --exclude-dir=/{var,Volumes,bin,cores,dev,sbin,tmp,usr} \
    -F 'The default interactive shell' /

I've also added -F since you're searching with a string, not a regular expression.

| improve this answer | |

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.