Stop fixing broken file permissions one by one with chmod
Permissions & Security (Chmod/Chown/ACLs/SELinux/AppArmor)
Stop fixing broken file permissions one by one with chmod
🧩 The Challenge
You finally realize your web app is throwing 403 errors because a rogue script created a file tree with completely butchered ownership and mode bits. Manual labor is for the birds, and running a recursive chmod on an entire directory is a great way to break every single binary inside the bin folder.
💡 The Fix
Use the reference flag on the chmod command to steal permissions from a known-good file or directory. It saves you from guessing octal modes or fumbling through man pages when you just want things to look like they did before the chaos started.
chmod --reference=/var/www/html/template_dir /var/www/html/broken_dir
find /var/www/html/broken_dir -type d -exec chmod --reference=/var/www/html/template_dir {} +
⚙️ Why It Works
This approach clones the exact bitmask from your source directory onto the target, ensuring your folders stay 755 and your files 644 without you having to run two separate commands. It is the cleanest way to restore sanity without nuking your security posture.
🚀 Pro-Tip: Combine this with find -type f to target just the files, or -type d for just the directories, if you need to be surgical.
Linux Tips & Tricks | © ngelinux.com | 9/23/2026
