shell 为大量文件/目录设置访问权限的最快方法?

hrysbysz  于 2023-08-07  发布在  Shell
关注(0)|答案(1)|浏览(106)

我想为大量文件和目录设置访问权限。在How to improve performance of changing files and folders permissions?https://superuser.com/a/91938/813482中,提供了几种设置权限的变体,其中包括:

变式一:

find /path/to/base/dir -type d -exec chmod 755 {} +
find /path/to/base/dir -type f -exec chmod 644 {} +

字符串

变式二:

find /path/to/base/dir -type d -print0 | xargs -0 chmod 755 
find /path/to/base/dir -type f -print0 | xargs -0 chmod 644

变式三:

chmod 755 $(find /path/to/base/dir -type d)
chmod 644 $(find /path/to/base/dir -type f)


其中哪一个应该是最有效的?在对我正在使用的目录进行的快速测试中,与变体1相比,变体2将时间从30秒以上减少到3秒以上(一个数量级),因为它不需要为每个文件分别调用chmod。变体3给出了警告,因为某些目录名/文件名包含空格,它无法访问这些目录。变体3甚至可能比变体2稍快,但我不确定,因为这可能与无法进入目录(?)).

5tmbdcev

5tmbdcev1#

其中哪一个应该是最有效的?
变式1.
如果你真的想要速度,穿越一次,而不是两次。

find /path/to/base/dir '(' -type d -exec chmod 755 {} + ')' -o '(' -type f -exec chmod 644 {} + ')'

字符串
如果文件或目录的数量大于平台上参数的最大数量,则变体2是很好的。
变体3非常糟糕,其中find的结果没有引号,shell将进行单词拆分和文件名扩展。如果任何路径有一个空间或一个星星,它将严重失败。

相关问题