I’m trying to properly manage file permissions on a Linux system and could use some guidance around using chmod directory settings correctly.
I have a main folder (for example, something like a web root directory), and when I run chmod on it, the permission change only applies to the top-level folder — not to the files and subfolders inside it.
For example, running a command like this:
chmod 775 /opt/example/htdocs
only updates the permissions of the htdocs directory itself, but none of its existing contents.
What I’m trying to achieve is:
- Set permissions (say
755) on all existing files and subdirectories inside that folder
- Make sure that new files and folders created in the future under this directory automatically get the correct permissions as well
What’s the correct and safe way to recursively update permissions for a directory and its contents using chmod?
And is there a recommended approach (like default permissions or umask) to ensure future files and folders inherit the right settings?
Any clear explanation or best practices would be really helpful. Thanks!
I’ve run into this issue a few times. The key is to use the -R (recursive) option with chmod. That way, the permission change applies to the directory itself and all files and subfolders under it. For example:
chmod -R 755 /opt/example/htdocs
This updates everything inside htdocs. Personally, I also like to double-check with ls -lR /opt/example/htdocs afterward to make sure the permissions propagated correctly. It’s simple and usually does the trick for existing files.
Sometimes I don’t want to give the same permissions to files and folders (for example, 755 for directories but 644 for files). In those cases, I use find to target them separately:
# Directories
find /opt/example/htdocs -type d -exec chmod 755 {} \;
# Files
find /opt/example/htdocs -type f -exec chmod 644 {} \;
I like this approach because it’s safer — you avoid accidentally giving executable permissions to files that shouldn’t have them, while still recursively updating everything.
For new files and folders, the system’s umask determines default permissions. For example, a umask of 002 ensures that new files are created with permissions like 664 and directories with 775. You can set it in your shell or in /etc/profile for system-wide defaults:
umask 002
Combining this with recursive chmod for existing content usually keeps your folder structure consistent and avoids permission headaches in the future. I’ve used this approach for web servers and shared project directories, and it works reliably.