How to CHMOD files and directories with the `find` command
How to Bulk CHMOD in Linux
To recursively CHMOD files and directories separately in Linux using the find command, use: find /path/to/dir -type f -exec chmod 644 {} \; for all files, and find /path/to/dir -type d -exec chmod 755 {} \; for all directories.
When running websites on Linux VPS or Dedicated Servers, setting correct file and directory permissions is mandatory to prevent HTTP 500 errors and severe security vulnerabilities. Doing this manually is impractical; thus, combining the find and chmod commands is the ultimate tool for any Sysadmin.
1. What is CHMOD in Linux?
CHMOD (Change Mode) is a command in Linux/Unix operating systems used to change the access permissions of files or directories. There are 3 fundamental permission groups you need to understand:
- Read (r - 4): The permission to read a file's content or list the contents of a directory.
- Write (w - 2): The permission to modify, change, or delete a file/directory.
- Execute (x - 1): The permission to run a file as a script or to access (cd) into a directory.
2. How to Check File Permissions in Linux
Before making bulk changes, verify your current source code's permission status using the long list command (ls -l) or the stat command:
ls -la /home/username/public_html
3. Why Use CHMOD 644 and 755?
In Web Hosting and VPS environments, the 644/755 structure is the "gold standard" for security:
- For Files (644): The Owner has Read & Write permissions (6). The Group and Others only have Read permissions (4). This prevents malicious actors from modifying your source code files (like PHP, HTML).
- For Directories (755): The Owner has full Read, Write & Execute permissions (7). Others have Read & Execute permissions (5), allowing the Web Server (Nginx/Apache) to enter the directory and serve its contents without the ability to delete the directory itself.
4. How to Bulk CHMOD Files and Directories
Here are the most practical find commands. Note: Remember to replace /home/username/public_html with the actual path on your server.
Method 1: Reset All Permissions to Standard
This action helps you clean up and reset your entire source code to standard permissions, properly separating files from directories.
find /home/username/public_html -type f -exec chmod 644 {} \;
find /home/username/public_html -type d -exec chmod 755 {} \;
Method 2: Fix Insecure Permissions
Instead of scanning everything, you can command find to only look for files with incorrect, dangerous configurations (e.g., 666 or 777) and fix them, saving system resources.
Fix files from 666/777 down to 644:
find /home/username/public_html -type f \( -perm 0777 -o -perm 0666 \) -exec chmod 644 {} \;
Fix directories from 777 down to 755:
find /home/username/public_html -type d -perm 0777 -exec chmod 755 {} \;
Method 3: Apply to All Users
If you are a Server Administrator (Root) and want to apply security standards across all user accounts in the /home directory, utilize the wildcard character *.
Apply to all directories:
find /home/*/public_html -type d -exec chmod 755 {} \;
Apply to all files:
find /home/*/public_html -type f -exec chmod 644 {} \;
5. How to use Recursive CHMOD (-R)
If you don't need to differentiate between file and directory permissions (e.g., granting full 777 access to a specific cache folder), you don't need find. Simply use the -R (Recursive) flag of the chmod command itself:
chmod -R 777 /home/username/public_html/wp-content/cache/
6. Common Errors & Important Warnings
Always ensure you terminate the -exec command with the string {} \; (the backslash is required to escape the semicolon in the Linux shell). Missing this will trigger the "find: missing argument to -exec" error.
Other common issues you might encounter:
- Operation not permitted (Permission denied): Occurs when you do not own the file/directory. Prepend your command with
sudo. - Accidental root directory CHMOD: Never run
find / -exec.... Incorrectly changing permissions on the root partition (/) will completely break the Linux operating system.
7. Conclusion
Using the find command paired with -exec chmod is the most professional, efficient, and powerful method to handle fragmented permission structures in Linux. Maintaining the 644/755 standard is your strongest defense against Local Attack exploits.
Want an environment that comes with 100% standardized security setups right out of the box? Depending on your performance needs, VietHosting has the ready-to-deploy infrastructure for your business.
More Technical Guides
Explore additional technical guides and practical tutorials to optimize performance and manage your server infrastructure efficiently.