How to Bulk set Permissions of Directories to 755 and Files to 644

Set Permissions bulk chmod

This guide will show you how to set permissions of all directories to 755 and all files to 644 in your web application for better security.

If you are not quite familiar with file or directory permissions then start by reading this article about chmod commands. There we explain how to set the required permissions on any Linux system. The article also explains what permissions 755 and 644 grant.

It is always advised to keep the file and directory permissions to minimal. As a general practice, many web application frameworks suggest that developers maintain permissions for all directories at 755 and all files at 644.

You may find that your web application has hundreds of files if not thousands and dozens of directories. It would be a nightmare to try and set the permissions of each file and directory individually.

The good news there is a better way.

How to Set Permissions Recursively

Navigate to the directory with cd command to get to the desired location under which you need to change the permissions. Such as:

$ /var/www/html

We will then run two commands. The first will find all directories in the path and change their permissions to 755 and the second will find all files and change their permissions to 644.

Read Also  dlib 19.3 Release is now Available for Download

$ find . -type d -exec chmod 755 {} +
$ find . -type f -exec chmod 644 {} +

You can also add the -name directive to change a specific file type. This example shows how to set permissions for only PHP files:

$ find . -type f -name "*.php" -exec chmod 644 {} +

With that you should be more efficient when changing file permissions.