Grep: excluding a specific folder using

Say our folder structure looks like this:

/app
/app/data
...
/app/secondary
/app/secondary/data

I want to search recursively /app/data, but I do not want to search /app/secondary/data.

From the application folder, what would my grep command look like?

+5
source share
2 answers

It will do the trick

grep -r --exclude-dir='secondary/data' PATTERN data
+11
source

Why the accepted answer is incorrect

@Seamus's accepted answer is incorrect because it grep -r --exclude-dir=<glob>matches base names that, by definition, cannot contain slashes (slashes make it no-op efficiently, since no base name will match).

From the GNU Grep Guide :

- exclude-dir = glob

... , glob...

, ,

, OP , , ...

.
β”œβ”€β”€ app
    β”œβ”€β”€ data
    └── secondary
        └── data

... app :

> grep -r <search-pattern> data

secondary data.

data, data, .

.
└── data
    β”œβ”€β”€ file.txt
    β”œβ”€β”€ folder1
    β”‚   └── folder2
    β”‚       β”œβ”€β”€ data
    β”‚       β”‚   └── file.txt
    β”‚       └── file.txt
    └── folder3
        β”œβ”€β”€ data
        β”‚   └── file.txt
        └── file.txt

> grep -r --exclude-dir='data' <search-pattern> data/*

* . , data .

+2

All Articles