Linux:仅对目录设置权限

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/17091300/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-06 23:12:37  来源:igfitidea点击:

Linux: Set permission only to directories

linuxbashdirectorychmod

提问by radicaled

I have to change the permissions of the htdocsdirectory in apache to a certain group and with certain read/write/execute.

我必须将htdocsapache 中目录的权限更改为某个组并具有特定的读/写/执行权限。

The directories need to have 775 permissions and the files need to have 664.

目录需要有 775 权限,文件需要有 664 权限。

If I do a recursive 664 to the htdocs, then all files and directories will change to 664.

如果我对 进行递归 664 htdocs,则所有文件和目录都将更改为 664。

I don't want to change the directories manually.

我不想手动更改目录。

Is there any way to change only files or directories?

有没有办法只更改文件或目录?

采纳答案by Barmar

Use find's -typeoption to limit actions to files and directories. Use the -ooption to specify alternate actions for different types, so you only have to run findonce, rather than separately for each type.

使用 find 的-type选项来限制对文件和目录的操作。使用该-o选项为不同类型指定替代操作,因此您只需运行find一次,而不是针对每种类型分别运行。

find htdocs -type f -exec chmod 664 {} + -o -type d -exec chmod 775 {} +

回答by Amit Verma

Use findto search for directories and apply chmod on them:

使用find搜索目录,并对其申请文件模式:

find -type d | xargs chmod 775

Use type ffor file:

使用f文件类型:

find -type f | xargs chmod 775

回答by RicardoE

try:

尝试:

find htdocs -type d -exec chmod 775 {} +

回答by Gordon Davisson

chmodcan actually do this itself; the Xsymbolic permission means "execute, if it makes sense" which generally means on directories but not files. So, you can use:

chmod实际上可以自己做到这一点;在X象征性的许可办法“执行,如果它是有道理的”一般是指在目录,但没有文件。因此,您可以使用:

chmod -R u=rwX,go=rX /path/to/htdocs

The only potential problem is that if any of the plain files already have execute set, chmodassumes it's intentional and keeps it.

唯一的潜在问题是,如果任何纯文件已经有执行集,则chmod假定它是故意的并保留它。

回答by MartinMlima

Gordon's answer above is correct, but if you're trying to lock down access to a directory tree, it leaves scripts that are executable to the owner also executable to whoever has been granted the capital X.

Gordon 上面的回答是正确的,但是如果你试图锁定对目录树的访问,它会留下对所有者可执行的脚本,也对被授予大写字母 X 的人执行。

Using

使用

find <path> -type d -exec chmod 775 {} +

or

或者

find <path> -type d -exec chmod 755 {} +

is safer.

更安全。

回答by genna

I use something similar to the solution provided by Gordon:

我使用类似于 Gordon 提供的解决方案的东西:

chmod -R ug=rw,o=r,a+X /path/to/folder/

It should always set 775 for folders and 664 for files, even if the execute permission was previosly set for some file

它应该始终为文件夹设置 775,为文件设置 664,即使之前为某些文件设置了执行权限