Kiran
Kiran

Reputation: 8548

How to exclude a directory in a recursive search using grep?

How to do a recursive search using grep while excluding a particular directory ?

Background : I have a large directory consisting of log files which I would like to eliminate in the search. The easiest way is to move the log folder. Unfortunately I cannot do that, as the project mandates the location.

Any idea how to do it ?

Upvotes: 21

Views: 17748

Answers (2)

OnlineCop
OnlineCop

Reputation: 4069

As an alternate, if you can use find in your search, it may also be useful:

find [directory] -name "*.log" -prune -o -type f -print|grep ...
The [directory] can actually be the current directory if you want (just a . will do).

The next part, -name "*.log" -prune is all together. It searches for filenames with the pattern *.log and will strip them OUT of your results.

Next is -o (for "or")

Then, -type f -print which says "print (to stdout) any type that is a file."

Those results should include every file (no directories are returned) found in [directory] except those that end in .log. Then you can grep the results as you need.

Upvotes: 1

Kent
Kent

Reputation: 195269

are you looking for this?

from grep man page:

--exclude-dir=DIR
            Exclude directories matching the pattern DIR from recursive searches.

Upvotes: 31

Related Questions