xargs + chmod - 找不到文件或目录

Bla*_*ack 3 permissions pipe find chmod xargs

我正在尝试递归更改项目中所有文件和目录的权限。

在 magento 论坛上发现一个帖子说我可以使用这些命令:

find ./ -type f | xargs chmod 644
find ./ -type d | xargs chmod 755
chmod -Rf 777 var
chmod -Rf 777 media
Run Code Online (Sandbox Code Playgroud)

它适用于find ./ -type d | xargs chmod 755.

该命令find ./ -type f返回了很多文件,但chmod: access to 'fileXY.html' not possible: file or directory not found如果我执行find ./ -type f | xargs chmod 644.

我该如何解决这个问题?

PS:我知道他建议我的var和media文件夹使用777权限,存在安全隐患,但我们还应该使用什么?

Ste*_*itt 6

我猜您遇到了名称包含导致xargs将它们拆分的字符的文件,例如空格。要解决这个问题,假设您使用的版本findxargs支持适当的选项(源自 GNU 变体,而不是由 POSIX 指定),您应该使用以下命令:

find . -type f -print0 | xargs -0 chmod 644
find . -type d -print0 | xargs -0 chmod 755
Run Code Online (Sandbox Code Playgroud)

或者更好,

chmod -R a-x,a=rX,u+w .
Run Code Online (Sandbox Code Playgroud)

它的优点是更短,仅使用一个进程,并且受POSIXchmod支持(有关详细信息,请参阅此答案)。

您的问题mediavar是相当广泛的,如果你有兴趣在具体的答案,我建议你问它与你的目录的用途是什么更多信息一个单独的问题。