Changing Directory and Files Permission Recursively in Linux

I’ve discovered the hidden treasure at superuser to change the permission of directories or files recursively in a folder. Not quite simple, but straight. To recursively give directories read & execute privileges:

find /path/to/base/dir -type d -exec chmod 755 {} +

To recursively give files read privileges:

find /path/to/base/dir -type f -exec chmod 644 {} +

Or, if there are many objects to process:

$ chmod 755 $(find /path/to/base/dir -type d)
$ chmod 644 $(find /path/to/base/dir -type f)

Or, to reduce chmod spawning:

$ find /path/to/base/dir -type d -print0 | xargs -0 chmod 755 
$ find /path/to/base/dir -type f -print0 | xargs -0 chmod 644

The Unreadable One

Another way, it makes you look like a Linux expert, just like this:

$ chmod -R u+rwX,go+rX,go-w /path

that’s unexplained. However, there are two notes you must read regarding this method because it’ll lead you to question more about this.

The important thing to note here is that X acts differently to x – the man page says The execute/search bits if the file is a directory or any of the execute/search bits are set in the original (unmodified) mode. In other words, chmod u+X on a file won’t set the execute bit; and g+X will only set it if it’s already set for the user.

This pattern won’t fix the situation when someone has done chmod -R 777 since the +X option will not reset existing execute bits on files. Using -x will reset directories, and prevent descending into them.