user855443
user855443

Reputation: 2940

find files and exclude some files and a directory

I want to find in a directory all files with extension .hs but exclude all files in a sub-directory sub and some other files with names containing test. I read and experimented with the use of find and prune but did not understand the complex logic and none of my attempts worked.

The naive

find . -name "*.hs" -not -name '*sub*' -not -name "*test*"

nor

find . -name "*.hs" -not -path '/sub' -not -name "*test*"  

does work. I assume there should be a simple solution to this (relatively) simple issue.

A solution that seems to work is

find . -name "*.hs"   -not -name "*test*"   | grep -v  "sub"

which is simpler than using prune, but can certainly be improved?

Upvotes: 0

Views: 151

Answers (1)

that other guy
that other guy

Reputation: 123460

Your first attempt excludes all files whose name includes sub.

Your second attempt excludes all files whose path is exactly /sub.

Combine the two to match all files whose path includes sub:

-not -path "*sub*"

However, -prune is the better solution because it skips the directory rather than fruitlessly matching every single entry in it.

Upvotes: 2

Related Questions