1

When I do chmod _+x -R /dir where "_" is any combination of (u,g,o,a), if after I do chmod g+X -R /dir, the files gain executable permissions as well.

Why does this happen? This behavior only happens if I use lower "x" first, then use upper "X".

First example:

[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

Here I only want to give group executable permission, but files also get executable permissions:

[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

Another example:

[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

Again, I only want to give group executable permission, but files gain executable permissions as well.

[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

Here is an example where "it works" but as you can see I did NOT use lower "x" prior to using upper "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
  • 3
    +X means "set execute bit if any of user/group/other already has execute bit set, or is directory". Why are you surprised that it sets the group execute bit on every file after you've explicitly set the user/other execute bit on every file? What are you trying to do that you expect -X to solve? – Michael Homer Apr 23 '16 at 3:38
  • @MichaelHomer I thought +X only affected directories. So if I use u+x -R, the execute bit is now "set" (for files AND directories ? or only files?) therefore any permission changes using +X will now affect files and directories? – jes516 Apr 23 '16 at 3:45
  • Yes – Michael Homer Apr 23 '16 at 3:49
  • @MichaelHomer Is there only 1 execute bit per file/directory? Or are there 3 (one for each option: u,g,o). I believe this is part of my confusion. – jes516 Apr 23 '16 at 3:53
  • There are three, one for each. – Michael Homer Apr 23 '16 at 3:56
3

+X means to set the execute bit:

if the file is a directory or if the current (unmodified) file mode bits have at least one of the execute bits (S_IXUSR, S_IXGRP, or S_IXOTH) set. It shall be ignored if the file is not a directory and none of the execute bits are set in the current file mode bits.

Once you've run chmod -R _+x dir, the execute bit is set for at least some of user/group/other for every file (that you have permission to modify). That means -X applies to all of those files as well.

If you only want to affect directories, and there are no other executable files in the tree, you can run the +X command before other modifications. Otherwise, you can use find:

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

That finds all directories (-type d) in dir and executes echo chmod g+x on them all, with {} turned into all the paths.

  • Perfect. Thank you very much. Also, why + at the end of the command and not ;? – jes516 Apr 23 '16 at 4:02
  • 2
    Because otherwise someone will moan at me that this single-use command is slightly inefficient (it would run chmod once per path, instead of once with as many files as possible). – Michael Homer Apr 23 '16 at 4:17
  • +1. I'll moan that you don't need to backslash-escape the +. you can't win :-) someone will always find something to moan about. – cas Apr 23 '16 at 4:22

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.