How to CHMOD files and directories with the `find` command
Incorrect file and directory permissions are a common issue that can lead to display errors or, more seriously, create security vulnerabilities. This article will guide you on how to use the find
command to bulk CHMOD permissions quickly and accurately.
Why Use CHMOD 644 for Files and 755 for Directories?
This is the standard and secure permission set for most web hosting environments:
1. Common CHMOD Use Cases
Below are commands you can use directly. Remember to replace /home/username/public_html
with your actual path.
Use Case 1: Reset All Permissions to Standard
This is the most common task when you want to clean up and ensure all source code has the correct permissions.
- Set all FILES to 644:
- Set all DIRECTORIES to 755:
Use Case 2: Fix Incorrect and Insecure Permissions
Sometimes, you only want to find and fix files/directories with specific incorrect permissions, such as 666 or 777.
- Find files with 666 permissions and change them to 644:
- Find directories with 777 permissions and change them to 755:
Use Case 3: Apply to All Users
If you are a server administrator and want to perform this for all user accounts in the /home
directory, you can replace the username with a wildcard character *
.
find /home/*/public_html -type f -exec chmod 644 {} ;
# CHMOD all directories for all users to 755
find /home/*/public_html -type d -exec chmod 755 {} ;
Important Warning
The find
command combined with -exec
is very powerful. Always double-check your path before executing the command, especially when using root privileges (sudo) or a wildcard (*). A small mistake can change permissions across your entire system and cause critical errors.