Chmod: Execute only if the file is a directory

1.3k views Asked by At

I understand that chmod -R u=rwX,g=rwX,o=r will set directory, subdirectories and files with the caveat:

The capital X permission option tells chmod to grant execute access only if it makes sense -- if the item is a directory, or already has at least one execute access enabled (i.e. if it appears to be an executable file).

But is there a way to force chmod (in a similar fashion) to only affect directories, leaving all files as-is?

— Source

1

There are 1 answers

2
Eugeniu Rosca On BEST ANSWER
find my/path -type d -exec chmod u=rwX,g=rwX,o=r {} \;
        +          +   +     +   +------+------+ +   +
        |          |   |     |          |        |   +-> semicolon needed by -exec and escaped to avoid shell expansion
        |          |   |     |          |        +-----> current directory entry returned by find
        |          |   |     |          +--------------> your chmod options
        |          |   |     +-------------------------> the shell command you want to execute on each directory entry
        |          |   +-------------------------------> need to execute a command for each entry returned by find
        |          +-----------------------------------> look for directories only (not files/symlinks/etc)
        +----------------------------------------------> the path to look for entries in