在使用 chmod 通过符号法来更改权限时,仅当文件是目录或者已经为用户、组或其他人设置了执行权限时,使用大写的 X 作为权限标志才会添加执行权限。
chmod 命令支持 -R 选项以递归方式对整个目录树中的文件设置权限。在使用 -R 选项时,使用 X 选项以符号形式设置权限会非常有用。这将能够对目录设置执行(搜索)权限,以便在不更改大部分文件权限的情况下,访问这些目录的内容。不过,使用 X 选项时要谨慎,因为如果某个文件设置有任何执行权限,则 X 也将会对该文件设置指定的执行权限。
Suppose you need to change permissions for some directories. Maybe the permissions are too strict, e.g. something like 700 for directories and 600 for files, and you need to give access to some other group and not just the owner.
As you know, execute permission on directories is required for traversal. So you might think doing something like:
chmod -R g+rx /var/somedirectory
This would probably work, but would leave you in a sad state: you'll get the executable bit set on files that are not supposed to be executable at all, which may be just annoying but may get risky as well.
A great alternative is:
chmod -R g+rX /var/somedirectory
I am sure you have already tried, at least once, to set permissions recursively on a directory and all of its contents. I am also sure you have wondered how you could recursively set the executable attribute on the subdirectories, which in this case means “enter”, but not on regular files. Well, this is what uppercase X does.