为什么 chmod 也会递归地更改文件权限?

为什么 chmod 也会递归地更改文件权限?

当我执行chmod _+x -R /dir其中“_”是(u,g,o,a)的任意组合时,如果在我执行之后chmod g+X -R /dir,文件也会获得可执行权限。

为什么会发生这种情况?仅当我先使用较低的“x”,然后使用较高的“X”时,才会发生此行为。

第一个例子:

[root@jesc5161 home]# chmod a-rwx -R finance/

[root@jesc5161 home]# ll
total 4
drwxr-xr-x. 3 root root      17 May  2  2015 ec2-user
d---------. 3 root finance   65 Apr 22 22:12 finance
drwx------. 9 user user    4096 Apr 22 21:28 user

[root@jesc5161 home]# ll finance/
total 0
d---------. 2 root root    6 Apr 22 21:41 accounting
----------. 1 user user    0 Apr 22 22:06 myfile1
----------. 1 user finance 0 Apr 22 22:12 myfile2
----------. 1 user user    0 Apr 22 22:12 myfile3

[root@jesc5161 home]# chmod u+x -R finance/

[root@jesc5161 home]# ll
total 4
drwxr-xr-x. 3 root root      17 May  2  2015 ec2-user
d--x------. 3 root finance   65 Apr 22 22:12 finance
drwx------. 9 user user    4096 Apr 22 21:28 user

[root@jesc5161 home]# ll finance/
total 0
d--x------. 2 root root    6 Apr 22 21:41 accounting
---x------. 1 user user    0 Apr 22 22:06 myfile1
---x------. 1 user finance 0 Apr 22 22:12 myfile2
---x------. 1 user user    0 Apr 22 22:12 myfile3

这里我只想给组可执行权限,但文件也获得可执行权限:

[root@jesc5161 home]# chmod g+X -R finance/

[root@jesc5161 home]# ll
total 4
drwxr-xr-x. 3 root root      17 May  2  2015 ec2-user
d--x--x---. 3 root finance   65 Apr 22 22:12 finance
drwx------. 9 user user    4096 Apr 22 21:28 user

[root@jesc5161 home]# ll finance/
total 0
d--x--x---. 2 root root    6 Apr 22 21:41 accounting
---x--x---. 1 user user    0 Apr 22 22:06 myfile1
---x--x---. 1 user finance 0 Apr 22 22:12 myfile2
---x--x---. 1 user user    0 Apr 22 22:12 myfile3

另一个例子:

[root@jesc5161 home]# chmod a-rwx -R finance/

[root@jesc5161 home]# ll
total 4
drwxr-xr-x. 3 root root      17 May  2  2015 ec2-user
d---------. 3 root finance   65 Apr 22 22:12 finance
drwx------. 9 user user    4096 Apr 22 21:28 user

[root@jesc5161 home]# ll finance/
total 0
d---------. 2 root root    6 Apr 22 21:41 accounting
----------. 1 user user    0 Apr 22 22:06 myfile1
----------. 1 user finance 0 Apr 22 22:12 myfile2
----------. 1 user user    0 Apr 22 22:12 myfile3

[root@jesc5161 home]# chmod u+rwx -R finance/

[root@jesc5161 home]# ll
total 4
drwxr-xr-x. 3 root root      17 May  2  2015 ec2-user
drwx------. 3 root finance   65 Apr 22 22:12 finance
drwx------. 9 user user    4096 Apr 22 21:28 user

[root@jesc5161 home]# ll finance/
total 0
drwx------. 2 root root    6 Apr 22 21:41 accounting
-rwx------. 1 user user    0 Apr 22 22:06 myfile1
-rwx------. 1 user finance 0 Apr 22 22:12 myfile2
-rwx------. 1 user user    0 Apr 22 22:12 myfile3

同样,我只想授予组可执行权限,但文件也获得可执行权限。

[root@jesc5161 home]# chmod g+X -R finance/

[root@jesc5161 home]# ll
total 4
drwxr-xr-x. 3 root root      17 May  2  2015 ec2-user
drwx--x---. 3 root finance   65 Apr 22 22:12 finance
drwx------. 9 user user    4096 Apr 22 21:28 user

[root@jesc5161 home]# ll finance/
total 0
drwx--x---. 2 root root    6 Apr 22 21:41 accounting
-rwx--x---. 1 user user    0 Apr 22 22:06 myfile1
-rwx--x---. 1 user finance 0 Apr 22 22:12 myfile2
-rwx--x---. 1 user user    0 Apr 22 22:12 myfile3

这是一个“它有效”的示例,但正如您所看到的,在使用上部“X”之前我没有使用下部“x”

[root@jesc5161 home]# chmod a-rwx -R finance/

[root@jesc5161 home]# ll
total 4
drwxr-xr-x. 3 root root      17 May  2  2015 ec2-user
d---------. 3 root finance   65 Apr 22 22:12 finance
drwx------. 9 user user    4096 Apr 22 21:28 user

[root@jesc5161 home]# ll finance/
total 0
d---------. 2 root root    6 Apr 22 21:41 accounting
----------. 1 user user    0 Apr 22 22:06 myfile1
----------. 1 user finance 0 Apr 22 22:12 myfile2
----------. 1 user user    0 Apr 22 22:12 myfile3

[root@jesc5161 home]# chmod a+rw -R finance/

[root@jesc5161 home]# ll
total 4
drwxr-xr-x. 3 root root      17 May  2  2015 ec2-user
drw-rw-rw-. 3 root finance   65 Apr 22 22:12 finance
drwx------. 9 user user    4096 Apr 22 21:28 user

[root@jesc5161 home]# ll finance/
total 0
drw-rw-rw-. 2 root root    6 Apr 22 21:41 accounting
-rw-rw-rw-. 1 user user    0 Apr 22 22:06 myfile1
-rw-rw-rw-. 1 user finance 0 Apr 22 22:12 myfile2
-rw-rw-rw-. 1 user user    0 Apr 22 22:12 myfile3

[root@jesc5161 home]# chmod g+X -R finance/

[root@jesc5161 home]# ll
total 4
drwxr-xr-x. 3 root root      17 May  2  2015 ec2-user
drw-rwxrw-. 3 root finance   65 Apr 22 22:12 finance
drwx------. 9 user user    4096 Apr 22 21:28 user

[root@jesc5161 home]# ll finance/
total 0
drw-rwxrw-. 2 root root    6 Apr 22 21:41 accounting
-rw-rw-rw-. 1 user user    0 Apr 22 22:06 myfile1
-rw-rw-rw-. 1 user finance 0 Apr 22 22:12 myfile2
-rw-rw-rw-. 1 user user    0 Apr 22 22:12 myfile3

[root@jesc5161 home]# chmod o+X -R finance/

[root@jesc5161 home]# ll
total 4
drwxr-xr-x. 3 root root      17 May  2  2015 ec2-user
drw-rwxrwx. 3 root finance   65 Apr 22 22:12 finance
drwx------. 9 user user    4096 Apr 22 21:28 user

[root@jesc5161 home]# ll finance/
total 0
drw-rwxrwx. 2 root root    6 Apr 22 21:41 accounting
-rw-rw-rw-. 1 user user    0 Apr 22 22:06 myfile1
-rw-rw-rw-. 1 user finance 0 Apr 22 22:12 myfile2
-rw-rw-rw-. 1 user user    0 Apr 22 22:12 myfile3

[root@jesc5161 home]# chmod u+X -R finance/

[root@jesc5161 home]# ll
total 4
drwxr-xr-x. 3 root root      17 May  2  2015 ec2-user
drwxrwxrwx. 3 root finance   65 Apr 22 22:12 finance
drwx------. 9 user user    4096 Apr 22 21:28 user

[root@jesc5161 home]# ll finance/
total 0
drwxrwxrwx. 2 root root    6 Apr 22 21:41 accounting
-rw-rw-rw-. 1 user user    0 Apr 22 22:06 myfile1
-rw-rw-rw-. 1 user finance 0 Apr 22 22:12 myfile2
-rw-rw-rw-. 1 user user    0 Apr 22 22:12 myfile3

答案1

+X方法设置执行位:

如果文件是目录,或者当前(未修改的)文件模式位至少设置了其中一个执行位(S_IXUSR、S_IXGRP 或 S_IXOTH)。如果文件不是目录并且当前文件模式位中没有设置任何执行位,则应忽略它。

运行后chmod -R _+x dir,将至少为每个文件(您有权修改)的某些用户/组/其他设置执行位。这意味着-X也适用于所有这些文件。

如果您只想影响目录,并且树中没有其他可执行文件,则可以+X在进行其他修改之前运行该命令。否则,你可以使用find:

find dir -type d -exec echo chmod g+x {} \+

-type d这会找到 ( ) 中的所有目录direcho chmod g+x对其执行所有操作,并{}转换为所有路径。

相关内容