The use of the uppercase X in chmod

I am aware that there are numerous guides about file permissions in linux out there. This post is not intended to be another tutorial. I just wanted to emphasize the use of uppercase X when modifying regular file or directory permissions. This info seems to be missing from most of those guides.

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.

For example, we issue the following chmod command on a directory:

# chmod -R u=rwX,g=rX,o=rX testdir/

Using the uppercase X, the above command sets the executable attribute according to the following two rules:

  1. If the file is a directory, then it sets the executable attribute for the owner, group and world, which means that they can enter this directory.
  2. If the file is a regular file, then it does not add the executable attribute to its permissions, unless it already has it enabled, in which case it retains it.

Using the lowercase x it would be impossible to achieve this result with one command only.

The use of the uppercase X in chmod by George Notaras is licensed under a Creative Commons Attribution-NonCommercial-ShareAlike 4.0 International License.
Copyright © 2005 - Some Rights Reserved

George Notaras avatar

About George Notaras

George Notaras is the editor of the G-Loaded Journal, a technical blog about Free and Open-Source Software. George, among other things, is an enthusiast self-taught GNU/Linux system administrator. He has created this web site to share the IT knowledge and experience he has gained over the years with other people. George primarily uses CentOS and Fedora. He has also developed some open-source software projects in his spare time.

5 responses on “The use of the uppercase X in chmod

  1. prism Permalink →

    It is not listed in any manual, because it is kind of unofficial. But it seems to work in every place I tried.

  2. George Notaras Post authorPermalink →

    I was not aware that it’s considered as an unofficial feature. At least, in Fedora it is clearly mentioned in the chmod man page. However, the use of find ... | xargs chmod ... is very convenient sometimes.

  3. Eric Wang Permalink →

    Nice blog, just one sentence is not quite clear, So, maybe it’s better to change
    “unless it already has it enabled, in which case it retains it.”
    to
    “when used on regular file, X would add on the x to the file only if any of u/g/o already has the x permission, if none of them has it, X is ignored.”

  4. Chris Magnuson Permalink →

    Just tested and confirmed that if the execute bit is set either for the user or the group when a a+X is run then it will add execute for everything else:

    [cmagnuson@node1 test]$ ls -l
    –wxr–r– 1 cmagnuson cmagnuson 24 Sep 18 18:55 test.sh
    [cmagnuson@node1 test]$ chmod a+X test.sh
    [cmagnuson@node1 test]$ ls -l
    –wxr-xr-x 1 cmagnuson cmagnuson 24 Sep 18 18:55 test.sh
    [cmagnuson@node1 test]$ chmod a-x test.sh
    [cmagnuson@node1 test]$ ls -l
    –w-r–r– 1 cmagnuson cmagnuson 24 Sep 18 18:55 test.sh
    [cmagnuson@node1 test]$ chmod a+X test.sh
    [cmagnuson@node1 test]$ ls -l
    –w-r–r– 1 cmagnuson cmagnuson 24 Sep 18 18:55 test.sh
    [cmagnuson@node1 test]$ chmod g+x test.sh
    [cmagnuson@node1 test]$ ls -l
    –w-r-xr– 1 cmagnuson cmagnuson 24 Sep 18 18:55 test.sh
    [cmagnuson@node1 test]$ chmod a+X test.sh
    [cmagnuson@node1 test]$ ls -l
    –wxr-xr-x 1 cmagnuson cmagnuson 24 Sep 18 18:55 test.sh
    [cmagnuson@node1 test]$