使用多个 exec 查找多个条件

Ang*_*elo 3 bash find

我正在尝试构建一个具有导致多个操作的多个条件的 find 命令。有人建议我只使用一个 find 命令以提高效率。这是我所拥有的:

    find $target \
            \( ! -group $project -exec chgrp $project "{}" \;       \) , \
            \( ! -user $owner -exec chown $owner "{}" \;            \) , \
            \( ! -perm "$perms" -exec chmod "$perms" "{}" \;        \) , \
            \( -type d -exec chmod g+s "{}" \; \)
Run Code Online (Sandbox Code Playgroud)

它似乎确实做了一些事情,但是我得到:

发现:无效的表达

(: 找不到相关命令

在我试图执行的脚本中

Gil*_*il' 6

错误消息表明它(是作为命令执行的,这意味着用于行继续的反斜杠之一实际上不是该行的最后一个字符。确保没有空格。确保您使用的是 Unix 行尾(仅 LF,无 CR)。

find关于无效表达式的抱怨是由于这些逗号。只需删除它们。

find "$target" \
        \( ! -group "$project" -exec chgrp "$project" {} \;   \) \
        \( ! -user "$owner" -exec chown "$owner" {} \;        \) \
        \( ! -perm "$perms" -exec chmod "$perms" {} \;        \) \
        \( -type d -exec chmod g+s {} \; \)
Run Code Online (Sandbox Code Playgroud)

您可能会节省每批文件运行一次命令而不是每个文件一次的时间。这在这里不能保证,因为chown,chgrpchmod调用可能会以不同的速率进行,因此目录条目可能会从缓存中逐出,但我会试一试。

find "$target" \
        \( ! -group "$project" -exec chgrp "$project" {} +   \) \
        \( ! -user "$owner" -exec chown "$owner" {} +        \) \
        \( ! -perm "$perms" -exec chmod "$perms" {} +        \) \
        \( -type d -exec chmod g+s {} + \)
Run Code Online (Sandbox Code Playgroud)

chgrpchmodchown什么也不做,如果该文件已经有了正确的元数据,所以你可以无条件的给他们打电话。然而,不必要地运行它们确实会导致更多的调用。实用程序将在调用stat后再次调用find,但 inode 很有可能仍在缓存中,因此这可能是值得的。您可以通过组合chgrp成来保存通话chown

find "$target" -exec chown "$owner:$project" {} + \
        -exec chmod "$perms" {} + \
        -type d -exec chmod g+s {} +
Run Code Online (Sandbox Code Playgroud)