Changing permissions on a single file is easy with chmod. Doing it across a whole directory tree – hundreds of files and folders – is where people get it wrong. The blanket command chmod -R 755 works, but it makes every file executable, which is incorrect for normal web files. This guide covers the recursive commands and, more importantly, the correct way to set files and directories to different permissions.
The Basics: chmod -R
The -R (recursive) flag applies chmod to a directory and everything inside it:
chmod -R 755 /path/directory
This sets every file and folder to 755. It’s fine for directories, but files usually shouldn’t be executable – so applying 755 to everything is not what you want for a website.
The Correct Way: Files 644, Directories 755
Standard, secure permissions are 644 for files (read/write for owner, read for others) and 755 for directories (which need the execute bit to be entered). Use find to apply each correctly:
# set all directories to 755: find /path/directory -type d -exec chmod 755 {} \; # set all files to 644: find /path/directory -type f -exec chmod 644 {} \;
This is the command experienced admins use. -type d matches directories, -type f matches files, so each gets the right permission in a single pass.
Changing Ownership Recursively (chown)
Permissions and ownership are separate. To recursively change the owner and group – for example to your web server user:
sudo chown -R www-data:www-data /path/directory
Replace www-data with the correct user for your setup (e.g. nginx, apache, or your username).
⚠
Never use chmod -R 777. It gives every user full read, write, and execute access, which is a serious security risk and a common cause of hacked sites. If something only works with 777, the real issue is ownership, not permissions — fix it with chown instead.
Quick Reference
FAQ
Q: How do I recursively change permissions in Linux? – Use chmod -R for a blanket change, or, better, use find with -type f and -type d to set files to 644 and directories to 755 separately.
Q: Why shouldn’t I use chmod -R 755 on everything? – Because it makes files executable, which is incorrect and insecure for normal web files. Files should be 644 and only directories 755.
Q: Is chmod 777 safe? – No. 777 gives everyone full access and is a common security hole. If a script needs it to work, the real problem is ownership – use chown instead.