Stop fighting with nested directory permissions using chmod -R

Permissions & Security (Chmod/Chown/ACLs/SELinux/AppArmor)

Stop fighting with nested directory permissions using chmod -R

🧩 The Challenge

You know the pain of trying to fix directory permissions with chmod -R, only to find out you’ve accidentally made every single file executable for everyone. I’ve ruined entire deployments this way because the standard recursive flag is a blunt instrument that doesn’t care if it’s touching a script or a text file.

💡 The Fix

Use the capital X flag instead of a hardcoded bit to target only directories and files that already have some form of execute permission. It saves you from accidentally marking your data files as runnable programs.

find /path/to/dir -type d -exec chmod 755 {} +
find /path/to/dir -type f -exec chmod 644 {} +
chmod -R u+rwX,go+rX /path/to/target

⚙️ Why It Works

Adding the capital X tells chmod to set the execute bit only if the file is a directory or already has an execute bit set for someone. This saves you from having to manually figure out which files actually need to be executable after a bulk change.

🚀 Pro-Tip: If you need to enforce this across a shared team folder, consider using default ACLs with setfacl so new files inherit the right bits automatically.

Linux Tips & Tricks | © ngelinux.com | 8/28/2026

0 0 votes
Article Rating
Subscribe
Notify of
guest

0 Comments
Newest
Oldest Most Voted