I'm trying to use a variable to specify an exclusion path in the find
command.
This snippet works:
x="*test/.util/*"
y="*test/sh/*"
find test -type f -name "*.sh" -not -path "$x" -not -path "$y"
But I'd like to move -not -path
into the variables, as in:
x="-not -path *test/.util/*"
y="-not -path *test/sh/*"
# error: find: -not -path *test/.util/*: unknown primary or operator
find test -type f -name "*.sh" "$x" "$y"
# Tried removing quotes
# error: find: test/.util/foo.sh: unknown primary or operator
find test -type f -name "*.sh" $x $y
I also tried adding quotes to the paths in the variables, but that results in no path filtering.
# no syntax error but does not exclude the paths
x="-not -path '*test/.util/*'"
y="-not -path '*test/sh/*'"
I'm using OSX Mavericks; GNU bash, version 3.2.51(1)-release (x86_64-apple-darwin13).
What am I doing wrong? Thanks
find
is receiving -not -path *test/.util/*
as a single argument, not as 3 separate arguments that it requires. You can use an array instead.
x=(-not -path "*test/.util/*")
y=(-not -path "*test/sh/*")
find test -type f -name "*.sh" "${x[@]}" "${y[@]}"
When quoted, ${x[@]}
expands into a series of separate words, one per array element, and each word remains properly quoted, so the patterns are passed literally, as intended, to find
.