Bash: 如何将 'find' 选项保存到变量中

Bash: How to save 'find' options into a variable

我需要 chownchmod 一些目录和文件。为了不总是重复查找过滤器,我需要将它们保存到一个变量中。

以下代码生成 Syntax error: "(" unexpected

findOpts=( \( -path ./.git -o -name .gitignore \) -prune -o )

find . "${findOpts[@]}" chown www-data:www-data {} \+

find . "${findOpts[@]}" -type d -exec chmod 550 {} \+
find . "${findOpts[@]}" -type f -exec chmod 440 {} \+

由于您的 chmod 调用只是在目录上启用 x 位并在文件中禁用它,您可以在 chmod 中使用 X(大写 x)(先清除它们之后):

chmod a=,ug+rX file[..]

此外,您可以在 find 中使用多个 -exec(至少在 GNU find 中),因此您可以只执行一次查找,而无需保存选项:

find . [your filters here] \
    -exec chown www-data:www-data {} \;  \
    -exec chmod a=,ug+rX {} \;