LF-DevJourney
LF-DevJourney

Reputation: 28564

grep how to exclude sub directory

I use grep to find a string and I want to exclude one directory with grep -rl --exclude-dir=application/res --colour 'super', but it also show match under directory application/res/.

While when I use grep -rl --exclude-dir=application --colour 'super', nothing matched under application path.

How can I to exclude one sub directory when use grep.

grep version:

$ grep -V
grep (GNU grep) 2.20

grep help:

  -r, --recursive           like --directories=recurse
  -R, --dereference-recursive
                            likewise, but follow all symlinks
      --include=FILE_PATTERN
                            search only files that match FILE_PATTERN
      --exclude=FILE_PATTERN
                            skip files and directories matching FILE_PATTERN
      --exclude-from=FILE   skip files matching any file pattern from FILE
      --exclude-dir=PATTERN directories that match PATTERN will be skipped.

Upvotes: 0

Views: 2317

Answers (1)

oguz ismail
oguz ismail

Reputation: 50815

Using find you can exclude a whole path with slashes in it:

find . -path ./application/res -prune -o -type f -exec grep -l super {} +

Despite being more portable, this will be slower than grep -r. But as far as I'm concerned, GNU grep doesn't provide a mechanism for excluding paths.

Upvotes: 2

Related Questions