This is intriquing and sorry I don't yet have a solution. I'm beginning to wonder if you need to use a script. My first thinking was that find would have an exclude path option; it does not. Actually the first thought was to just try what you did, with a suspicion that it would not work. It does not, and suggest you also try it.
From some top level, I have ./1.txt and then made sub-directories 'a' and 'b' and placed 2.txt and 3.txt in each, and then did a find for all "*.txt" from that top level. A normal find, of course then finds all three of those txt files.
This is what I see in the manpage for find under the section about the -path option:
Code:
To ignore a whole directory tree, use -prune rather than
checking every file in the tree. For example, to skip the directory `src/emacs' and all files and directories under it, and print
the names of the other files found, do something like this:
find . -path ./src/emacs -prune -o -print
So as a result, the find for me might be something like the following to exclude the 'a' sub-directory:
Code:
find . -name "*.txt" -path ./a -prune -o -print
The problem is, that doesn't work in fact it so anti-works it's almost manic. What that command does is also finds other files not matching "*.txt" and it also finds files in the 'a' sub-directory.
Seeing as the man page's example did not use a pattern I therefore chose to use their supplied example almost verbatim:
Code:
find . -path ./a -prune -o -print
THAT actually does exclude the sub-directory as specified. The problem I see there is that it won't accept a file pattern match, because as soon as I put in the -name directive, it goes back to finding all files.
Here's the full run:
Code:
~/testcode$ ls -R
.:
2.txt a b myfile.txt test.c signed signed.c whle.sh
./a:
a.txt
./b:
b.txt
## So that's everything in my current directory, and below
~/testcode$ find . -name "*.txt"
./a/a.txt
./myfile.txt
./b/b.txt
./2.txt
## That's all "*.txt" in my current directory, and below
~/testcode$ find . -path ./a -prune -o -print
.
./of_test.c
./signed
./signed.c
./myfile.txt
./b
./b/b.txt
./whle.sh
./2.txt
## This is their default -prune method, as you can see it excludes ./a
~/testcode$ find . -path ./a -prune -o -print -name "*.txt"
.
./of_test.c
./signed
./signed.c
./myfile.txt
./b
./b/b.txt
./whle.sh
./2.txt
## This still excludes ./a however it doesn't perform file pattern match for "*.txt"
~/testcode$ find . -name "*.txt" -path ./a -prune -o -print
.
./of_test.c
./signed
./a
./a/a.txt
./signed.c
./myfile.txt
./b
./b/b.txt
./whle.sh
./2.txt
## This is actually WORSE, it somehow disables the -prune action and also doesn't limit to "*.txt"
Sorry, this is "halfway" and I'm wondering if there is a real one-line "find" solution that does work.