226

How can I recursively chmod everything inside of a folder?

e.g. I have a folder called var which contains many subfolders and files.

How can I apply chmod 755 recursively to this folder and all its contents?

1

5 Answers 5

320

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.

5
  • 16
    When adding permission bits, uppercase [augo]+X is supported to only add +x if the object is already executable. May 23, 2018 at 10:21
  • It is not the var folder from linux, but from my website. But still a good hint!
    – Black
    May 23, 2018 at 10:38
  • 1
    @Black Sure, I just wanted to add a big caveat. For websites in particular (most CMSes like WordPress or Drupal want that), the permissions of directories and files should be different for security reasons, see e.g. here for WordPress.
    – slhck
    May 23, 2018 at 10:40
  • @grawity - I can't believe I've not noticed +X before: it's so useful with multiple files, whether from -R, a file mask, or in processing files found with find. A really useful tip.
    – AFH
    May 23, 2018 at 10:43
  • for me man chmod does not give recursive, but -R: Change the modes of the file hierarchies rooted in the files, instead of just the files themselves. Beware of unintentionally matching the “..” hard link to the parent directory when using wildcards like “.*”.. That's a weird description.
    – gernophil
    Dec 2, 2023 at 6:55
44

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 {} \;

7

If you wish to apply chmod to a specific directory/ file you can make use of find as following:

find . -type f -name "*.sh" -print0 |xargs -0 chmod 755
1

To set the rights for all files (to 644) and all directories (to 755) in your directory YOUR_CATALOG at once you can use this:

find YOUR_CATALOG -type f -exec chmod 664 {} + -o -type d -exec chmod 775 {} +
0

You can give all permission for your localhost

sudo chmod -R goa=rwx /path/to/directory
1
  • isnt this the same as chmod 777 ??
    – Black
    Sep 22, 2021 at 8:41

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .