设置大量文件/目录的访问权限的最快方式是什么?

huangapple go评论57阅读模式
英文:

Fastest way to set access permissions for a large number of files / directories?

问题

变体1:

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

变体2:

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

变体3:

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稍快,但我不确定,因为这可能与无法进入目录有关(?)。

英文:

I want to set access permissions for a large number of files and directories. In https://stackoverflow.com/q/30592771/5269892 and https://superuser.com/a/91938/813482, several variants to set permissions are presented, among others:

Variant 1:

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

Variant 2:

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

Variant 3:

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

Which of these should be the most efficient? In a quick test for the directory I'm using, compared to variant 1, variant 2 reduced the time from more than 30 to more than 3 sec (one order of magnitude), since it does not need to call chmod for every file separately. Variant 3 gave warnings, since some directory names/filenames contain spaces and it cannot access those directories. Variant 3 may even be slightly faster than variant 2, but I'm not sure since this may be related to not being able to enter the directories(?).

答案1

得分: 1

Variant 1.

如果你真的想要速度,只需遍历一次,而不是两次。

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

Variant 2 在文件或目录数量大于平台上的最大参数数量时非常高效。

Variant 3 非常糟糕,因为没有引用find的结果,shell 将执行单词拆分和文件名扩展。如果任何路径中包含空格或星号等字符,它将失败。

英文:

> Which of these should be the most efficient?

Variant 1.

I you really want speed, traverse once, not twice.

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

Variant 2 is great if the number of files or directories is greater than the maximum number of arguments on a platform.

Variant 3 is very bad, where the result of find is not quoted, and the shell will do word splitting and filename expansion. It will fail badly if any path has, for example, a space or a star.

huangapple
  • 本文由 发表于 2023年7月17日 23:10:25
  • 转载请务必保留本文链接:https://go.coder-hub.com/76705835.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定