How do I chmod everything in a folder?
Add -R:
chmod -R g-w /srv/reports
This removes group write from /srv/reports and every file and folder inside it. Symbolic changes like this one, which add or remove a single bit, are safe recursively because they keep everything else as it was. Problems start when you set an absolute numeric mode on a mixed tree.
Why is chmod -R 755 or 644 a trap?
Files and folders need different bits. Folders need execute so anyone can enter them; most files must not have it. chmod -R 644 site/ removes execute from every folder, and the whole tree becomes unreachable, even for the owner's processes. chmod -R 755 site/ does the opposite and marks every image and config file as a program. Both are common mistakes after a failed deploy.
What is the capital X trick?
In symbolic modes, X means "execute, but only on folders and on files that already have an execute bit for someone". One command then fixes a typical web tree:
chmod -R u=rwX,g=rX,o= /srv/app # folders 750, files 640, existing scripts 750 chmod -R u=rwX,go=rX /var/www/blog # folders 755, files 644
Existing executables such as vendor/bin/* keep their execute bit, which a blanket 644 would have stripped.
How do I treat files and folders separately with find?
When you want explicit numbers, let find pick the type:
find /srv/app -type d -exec chmod 750 {} +
find /srv/app -type f -exec chmod 640 {} +
find /srv/app/bin -type f -name '*.sh' -exec chmod 750 {} +
The {} + ending passes many paths per chmod call, which is far faster than {} \; on big trees. On a machine with NVMe drives, such as any model from S1 up, these commands finish in seconds even for hundreds of thousands of files.
How can I target only some files?
| Goal | Command |
|---|---|
| Only files with a certain extension | find /srv/app -type f -name '*.env' -exec chmod 600 {} + |
| Only files writable by others | find /srv -perm -o+w -type f -exec chmod o-w {} + |
| Only files owned by one user | find /srv -user olek -type d -exec chmod g+s {} + |
| Only recently changed files | find /srv/uploads -mmin -60 -type f -exec chmod 644 {} + |
What happens with symbolic links?
chmod never changes the mode of a symlink itself; Linux ignores link modes. When chmod -R walks a tree it does not follow symlinks to folders by default in GNU coreutils, so links pointing outside the tree are left alone. That protects you from a link to / turning a small chmod into a system-wide one. Be careful with find -L or chmod -R -L, which do follow links.
How do I avoid changing the whole system by accident?
GNU chmod refuses to recurse on / by default thanks to --preserve-root. A variable that expands to nothing is the bigger danger: chmod -R 750 $APPDIR/ with an empty APPDIR becomes chmod -R 750 /, which the default protection does catch, but the same mistake inside a deeper path may not. In scripts, write "${APPDIR:?}" so the shell stops when the variable is empty, and use set -u.
How do I undo a bad recursive chmod?
There is no undo button, so the plan depends on what you have:
- Application folders: reapply the correct modes with the find pairs above, or redeploy from version control.
- Package-owned system files: on Debian and Ubuntu,
dpkg --verifylists files whose modes differ, and reinstalling the package restores them. On Enterprise Linux,rpm --setperms -aresets modes of every packaged file, andrpm -Valists what differs. - Everything else: restore from a backup made with tools that keep modes, such as rsync
-aor tar.
Before big changes, save the current modes so you can put them back: getfacl -R -p /srv/app > /root/app-modes.acl, restored later with setfacl --restore=/root/app-modes.acl.
How do I check the result?
find /srv/app \( -type d -not -perm 750 \) -o \( -type f -not -perm 640 -not -perm 750 \) | head
Anything listed has a mode outside the plan. Ownership usually needs the same treatment; see chown recursively.
Keep Reading
The chmod command
Change file modes with numbers or letters, understand what each bit does and use special bits like setgid and sticky correctly.
Openchmod 755 explained
What mode 755 grants, where it belongs on a server, where it does not, and how it compares with 750, 775 and 644.
Openchown recursively
Change owners across a folder tree with chown -R, choose how symlinks are handled and limit changes to files owned by one account.
OpenLinux file permissions
Read ls -l output, understand owner, group and other bits, umask defaults, ACLs and why root and SELinux sit on top of it all.
OpenFAQ
Quick Answers
Use chmod -R with a symbolic mode, or find with -type d and -type f to set different modes for folders and files.
Execute only for folders and files that already have an execute bit, which makes recursive changes safe.
It removed execute from folders, so nobody can enter them. Restore 755 on folders with find -type d.
GNU chmod does not follow symlinks to folders by default when recursing.
Use dpkg --verify and reinstall on Debian and Ubuntu, or rpm --setperms on Enterprise Linux.