MichaelSB
MichaelSB

Reputation: 3181

Excluding directories with grep

I just tried:

michael@Pascal:~/noisynet$ sudo grep -rio --exclude-dir={/ece,/home/michael/pytorch,/sys,/proc} 'hello' /

The very first match is:

/home/michael/pytorch/.git/logs/HEAD:hello

Why is it looking in /home/michael/pytorch?

Upvotes: 10

Views: 14125

Answers (1)

Avinash Yadav
Avinash Yadav

Reputation: 916

This will work well

grep -rio --exclude-dir={ece,pytorch,sys,proc} 'hello' /

Note: This will also exclude other directories with same name.

Explanation:

Man page of grep gives below snippet

   --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.

This means given pattern (GLOB) will be applied only to the actual name of the directory, and since a directory name don't contain / in its name, a pattern like /proc will never match.

Hence, we need to use --exclude-dir=proc or --exclude-dir=sys (or --exclude-dir={proc,sys}) just names for directories to be excluded without '/'.

Upvotes: 18

Related Questions