Reputation: 116283
I have a directory ./src
with files. I would like to extract all .c
files in it, except those .c
files that are in ./src/test
. I have tried variants of
find ./src -name "*.c" -and -not -name ./src/test
(inspired from here) but none have succeeded.
What am I doing wrong?
Upvotes: 1
Views: 259
Reputation: 2061
You want -regex
to match the entire path (-name
only matches the filename in the current directory) and -prune
to eliminate that part of the tree:
find ./src -regex '^./src/test$' -prune -o -name '*.c' -print0 | xargs -0 ....
find
without -print0
is almost always a problem waiting to happen if someone creates a filename with a space in it (or worse a newline).
Upvotes: 2
Reputation: 753725
I'd use a grep -v
post-filter, assuming you don't have newlines in your paths (spaces will be OK):
find ./src -name "*.c" | grep -v '/src/test/'
I think your trouble is that the -name
looks at the last element of the path only, so a -name
with slashes in it simply doesn't work.
If your version of find
supports it, using -path
in place of -name
might work:
find ./src -name "*.c" -and -not -path './src/test/*'
Note the modified operand to -path
.
Upvotes: 3
Reputation: 15256
Can you use grep
?
find ./src -name \*.c | grep -v ./src/test
Upvotes: 3