Give execution permission to directories but not to files

Solution 1:

To give execution (search) permission to directories, but not to files, use:

chmod -R +X .

To assign all the permissions as in your example, use:

chmod -R u=rwX,g=rX,o= .

-R changes files and directories recursively, while +X sets execute/search only if the file is a directory or already has execute permission for some user. r and w are of course for reading and writing, respectively.

Mode X (upper x) is documented in both the traditional manual page1 and the info documentation2.

It should also work on other Unix like systems, e.g. FreeBSD, NetBSD or OpenBSD. Quoting from the chmod(1) man page of The Open Group Base Specifications Issue 7, 2018 edition:

The X perm symbol was adopted from BSD-based systems because it provides commonly desired functionality when doing recursive (-R option) modifications. Similar functionality is not provided by the find utility. Historical BSD versions of chmod, however, only supported X with op+; it has been extended in this volume of POSIX.1-2017 because it is also useful with op=. (It has also been added for op- even though it duplicates x, in this case, because it is intuitive and easier to explain.)


1man 1 chmod
2info '(coreutils)Conditional Executability'

Solution 2:

If you want to do it recursively, i.e., to directories within directories within directories, the command to use is:

 find /path/to/starting/directory -type d -exec chmod +x {} \;

This locates all and only subdirectories (-type d flag) of the directory /path/to/starting/directory, and then performs the required change of execute permission to each one of them. The space before \; is mandatory.