Linux: Set permission only to directories

こ雲淡風輕ζ 提交于 2019-12-02 19:22:54

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

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

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

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, chmod assumes it's intentional and keeps it.

Amit

Use find to search for directories and apply chmod on them:

find -type d | xargs chmod 775

Use type f for file:

find -type f | xargs chmod 775

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.

Using

find -type d exec chmod 775 {} +

or

find -type d exec chmod 755 {} +

is safer.

RicardoE

try:

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

I use something similar to the solution provided by 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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!