Linux - How to recursively chmod a folder?

62

12

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?

Black

Posted 2018-05-23T10:13:27.687

Reputation: 3 900

Answers

102

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.

slhck

Posted 2018-05-23T10:13:27.687

Reputation: 182 472

6When adding permission bits, uppercase [augo]+X is supported to only add +x if the object is already executable. – user1686 – 2018-05-23T10:21:29.093

It is not the var folder from linux, but from my website. But still a good hint! – Black – 2018-05-23T10:38:29.203

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 – 2018-05-23T10:40:08.727

@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 – 2018-05-23T10:43:55.860