有没有一种方法可以将选项作为数组传递给bash中的查找命令?

时间:2019-01-19 07:25:40

标签: arrays bash find gnu-findutils

rsync中,我只可以在数组中定义排除项/选项,并在后续命令(例如e)中使用这些数组。 G。

rsync "${rsyncOptions[@]}" "${rsyncExclusions[@]}" src dest

我想使用find命令来实现相同的目的,但是我找不到使它起作用的方法:

findExclusions=(
    "-not \( -name '#recycle' -prune \)"
    "-not \( -name '#snapshot' -prune \)"
    "-not \( -name '@eaDir' -prune \)"
    "-not \( -name '.TemporaryItems' -prune \)"
)
LC_ALL=C /bin/find src -mindepth 1 "${findExclusions[@]}" -print

无论如何尝试定义数组(单引号,双引号,转义括号),我总是以错误结尾:

find: unknown predicate `-not \( -name '#recycle' -prune \)'
# or
find: paths must precede expression: ! \( -name '#recycle' -prune \)

正确的方法是什么?

1 个答案:

答案 0 :(得分:4)

这里的问题是"-not \( -name '#recycle' -prune \)"不是一个参数,而是6个参数。您可以这样写:

findExclusions=(
    -not '(' -name '#recycle' -prune ')'
    -not '(' -name '#snapshot' -prune ')'
)
相关问题