Managing file and folder permissions is a fundamental skill for any Linux administrator. In Ubuntu, the chmod command is used to modify these read, write, and execute permissions. However, a very common mistake occurs when users try to change permissions for an entire directory tree.
If you use the standard recursive command (chmod -R 755 /your/folder), you will apply the exact same permissions to both files and directories. This is a massive security and functional problem. Directories need the execute bit (the 1 in the 755) so you can enter them, but standard files (like text documents or images) should almost never be executable.
To safely change permissions for a directory and its subdirectories—while treating files differently than folders—you must combine chmod with the find command.
The Correct Method: Using find and exec
The standard best practice for web servers and local file shares is to set directories to 755 (read/write/execute for the owner, read/execute for others) and files to 644 (read/write for the owner, read-only for others).
Instead of running a blanket recursive command, we will use find to filter by file type and apply the correct chmod exclusively to that type.
Step 1: Set Permissions for Directories Only
Open your terminal and run the following command, replacing /path/to/your/folder with the actual path to your target directory:
sudo find /path/to/your/folder -type d -exec chmod 755 {} +
How this command works:
find /path/to/your/folder: Tells the system exactly where to start searching. It automatically checks all subdirectories.-type d: Filters the results so it only targets directories.-exec chmod 755: Executes the chmod command to set the 755 permission.{} +: This is a highly efficient placeholder. It gathers all the matching directories and passes them to chmod in one massive batch, rather than running chmod thousands of individual times.
Step 2: Set Permissions for Files Only
Now that the directories are secure, you can set the permissions for the files contained within them.
sudo find /path/to/your/folder -type f -exec chmod 644 {} +
How this command works:
The syntax is identical to the previous step, except for the -type f flag, which tells Ubuntu to only target standard files. The 644 permission ensures they can be read and modified by you, but they cannot be executed as scripts by malicious actors.
Why You Should Avoid chmod -R
You will often see tutorials online suggesting the following command to fix permission errors:
sudo chmod -R 777 /var/www/html
This is a catastrophic security vulnerability. The -R flag applies the exact same permission to every single file and folder in the tree.
- If you use
-R 777, you are giving anyone on the system (or anyone who accesses a vulnerability in your web server) the ability to edit, delete, or execute any file. - If you use
-R 755, you are unnecessarily making all your text documents and images executable. - If you use
-R 644, your files will be secure, but you will instantly lock yourself out of all your directories (because directories require the execute bit to be opened).
By taking an extra few seconds to use the find command, you guarantee that your Ubuntu system remains both secure and fully functional.