Home  »  ArticlesGuidesHow ToTechnology   »   How to Bulk set Permissions of Directories to 755 and Files to 644

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

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 is there is a better way.

How to Set Permissions Recursively

Navigate to the directory with the 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.

$ 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.

Found this article interesting? Follow Brightwhiz on Facebook, Twitter, and YouTube to read and watch more content we post.

Available under:
Articles, Guides, How To, Technology