Linux - How to recursively chmod a folder?

Please refer to the manual (man chmod):

-R, --recursive
change files and directories recursively

chmod -R 755 /path/to/directory would perform what you want.

However…

  1. You don't usually want to 755 all files; these should be 644, as they often do not need to be executable. Hence, you could do find /path/to/directory -type d -exec chmod 755 {} \; to only change directory permissions. Use -type f and chmod 644 to apply the permissions to files.

  2. This will overwrite any existing permissions. It's not a good idea to do it for /var — that folder has the correct permissions set up by the system already. For example, some directories in /var require 775 permissions (e.g., /var/log).

So, before doing sudo chmod — particularly on system folders — pause and think about whether that is really required.


For a PHP-based web site, many sources like this one recommend 755 for directories and 644 for files.

If you are in the DocumentRoot of the website, you can set this as follows:

find . -type d -exec chmod 755 {} \;

find . -type f -exec chmod 644 {} \;

Tags:

Chmod