linux,

How to change permissions for Directories and Files recursively

Oct 25, 2021 · 1 min read · Post a comment
How to change permissions for Directories and Files recursively

Getting to know how to fix the directories and files permissions recursively, with a quick and easy method is always a good point. The crux of this tutorial is to understand how you can separately change the directories permissions and files as well. Let’s go to the solution.

Prerequisites

  • Linux based system

Solution

Step 1. Let’s say we want to fix only the directories to 755 under /var/www/html:

find /var/www/html -type d -exec chmod 755 {} \;

Step 2. To do the same fix only for the files (644) under the same path /var/www/html:

find /var/www/html -type f -exec chmod 644 {} \;

Step 3. Explanation of the find command:

  • chmod 755 {} - It’s going to be executed by find for each directory under the specified path.
  • chmod 644 {} - It’s going to be executed by find for each file under the specified path.
  • \; - It tells to escape the semicolon instead of being executed by the shell.
  • type - whether it’s a file (f) or directory (d).

Conclusion

The path is the only section that may vary depending on your case, the rest of the command is just a simple copy-paste job that will quickly help you to fix the permissions. Feel free to leave a comment below and if you find this tutorial useful, follow our official channel on telegram.