How to Set 644 for Files and 755 for Directories in Linux

Quick Linux Tip #79:

Question: How do I set 644 for files and 755 for directories in one shot?

Try: find /var/www/site -type d -exec chmod 755 {} + && find /var/www/site -type f -exec chmod 644 {} +

Info: Uses -type d to grant directories execute permissions (755) and -type f to grant files read/write permissions (644). Using + batches executions efficiently.

Examples:

  • $ find /path -type d -exec chmod 750 {} +  # Restrict directory permissions to owner/group
  • $ find /path -type f -name '*.sh' -exec chmod +x {} +  # Bulk grant execute to shell scripts
  • $ chmod -R u=rwX,g=rX,o=rX /path  # Apply permissions recursively with capital X

Note: Running chmod -R 644 strips directory execute permissions and prevents directory traversal. Capital X in symbolic mode, as used in chmod -R u=rwX,g=rX,o=rX /path, applies execute permission only where appropriate, such as directories and files that already have an execute bit.




LinuxTeck.com
linuxteck:~$ find /var/www/site -type d -exec chmod 755 {} + && find /var/www/site -type f -exec chmod 644 {} +

linuxteck:~$ ls -la /var/www/site
total 24
drwxr-xr-x 5 www-data www-data 4096 Oct 09 10:23 .
drwxr-xr-x 3 root     root     4096 Oct 01 09:00 ..
-rw-r--r-- 1 www-data www-data 2345 Oct 09 10:21 index.html
-rw-r--r-- 1 www-data www-data 4567 Oct 09 10:21 config.php
drwxr-xr-x 2 www-data www-data 4096 Oct 09 10:22 css
drwxr-xr-x 2 www-data www-data 4096 Oct 09 10:22 js
drwxr-xr-x 2 www-data www-data 4096 Oct 09 10:22 images
linuxteck:~$

PREVIOUS ARTICLE Quick Linux Tip #78: How to Monitor Network Interface Traffic in Linux with sar NEXT ARTICLE Quick Linux Tip #80: How to Find Large Files Modified Recently in Linux
About John Britto

John Britto Founder & Chief-Editor @LinuxTeck. A Computer Geek and Linux Intellectual having more than 20+ years of experience in Linux and Open Source technologies.

View all posts by John Britto →

Leave a Reply

Your email address will not be published.