Chmod

為什麼 chmod 也會遞歸地更改文件權限?

  • April 23, 2016

當我在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

+X表示設置執行位:

如果文件是目錄,或者目前(未修改的)文件模式位至少設置了一個執行位(S_IXUSR、S_IXGRP 或 S_IXOTH)。如果文件不是目錄並且在目前文件模式位中沒有設置任何執行位,則應忽略它。

執行chmod -R _+x dir後,將為每個文件(您有權修改)至少設置一些使用者/組/其他的執行位。這意味著-X也適用於所有這些文件。

如果您只想影響目錄,並且樹中沒有其他執行檔,則可以+X在其他修改之前執行該命令。否則,您可以使用find

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

這會找到所有目錄(-type ddir並在所有目錄上執行echo chmod g+x,並{}變成所有路徑。

引用自:https://unix.stackexchange.com/questions/278512