使递归 chmod 更快

Ben*_*rel 5 linux chmod

我有一个unzipsa 目录的安装脚本,然后递归地chmods 其内容。

我很惊讶运行以下两个命令所需的时间几乎是解压缩时间的 10 倍:

find $dir -type f -exec chmod a+r "{}" \;
find $dir -type d -exec chmod a+rx "{}" \;
Run Code Online (Sandbox Code Playgroud)

我做错了什么,有没有更快的方法来更改所有文件和目录的 chmod?

mgo*_*ven 10

您可以find使用 chmod 的X标志来摆脱这些命令:

execute/search only if the file is a directory or already has execute permission for some user (X)
Run Code Online (Sandbox Code Playgroud)

这允许您使用单个命令对文件和目录设置相同的权限,目录另外是可执行的:

$ chmod -R a+rX $dir
Run Code Online (Sandbox Code Playgroud)


mdp*_*dpc 5

您可以执行以下操作(假设为 Linux):

cd $dir
chmod -R a+r .
find . -type d -print0 | xargs -0 chmod a+x
Run Code Online (Sandbox Code Playgroud)

这应该比您在问题中指出的两个命令的组合快得多。