Linux: Set permission only to directories

2020-05-14 09:41发布

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

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

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

I don't want to change the directories manually.

Is there any way to change only files or directories?

6条回答
Animai°情兽
2楼-- · 2020-05-14 10:18

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 {} +
查看更多
Summer. ? 凉城
3楼-- · 2020-05-14 10:20

try:

find htdocs -type d -exec chmod 775 {} +
查看更多
你好瞎i
4楼-- · 2020-05-14 10:32

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

查看更多
爱情/是我丢掉的垃圾
5楼-- · 2020-05-14 10:32

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 <path> -type d -exec chmod 775 {} +

or

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

is safer.

查看更多
ら.Afraid
6楼-- · 2020-05-14 10:36

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.

查看更多
闹够了就滚
7楼-- · 2020-05-14 10:36

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
查看更多
登录 后发表回答