Linux - Change Permissions Recursively
I will not go long, but never, never, ever set file permissions to 0777 on production servers (or for that matter any server). This leads to WORLD writable and leads to security issues, including take-over/spamming and what not.
Always keep the file and directory permissions to minimal. Many applications frameworks request/suggest to keep permissions for all directories to 0755, and all files to 0644.
So, let us try that out and do it smartly this time.
Change Permissions Recursively
Change directory with cd command to the desired location under which you need all directories to have the permission level to 0755, and all files to 0644.
cd /home/user/public_html
Then use the first command below to chmod 0755 for all directories and sub directories. The second command will change all the files permission to 0644 (chmod 0644) under the directory tree.
find . -type d -exec chmod 0755 {} ; find . -type f -exec chmod 0644 {} ;
You can also change permission using xargs command to do this quickly.
find . -type d -print0 | xargs -0 chmod 755
find . -type f -print0 | xargs -0 chmod 644
Here the directory permission 0755 is similar to “rwxr-xr-x” and the file permission 0644 is equal to “rw-r–r–“.
Change Permission for Specific files
Instead of changing permission for all files, you can also target the specific files with similar extensions. For example, if you have a PHP application on your server, & you don’t want to allow others to execute the PHP files, then use the following command to chmod 0640 all of those files with php extension:
find . -type f -name "*.php" -exec chmod 0640 {} ;
The file permission 0640 will restrict others with no permissions. This adds an extra layer of security under permissions.