You have a web application hosted on a server and you want to set file access permissions by chmod command, different for files and folders.
Why different? A folder must have ‘x’ (access) flag:
drwxr-xr-x
At the same time a file shouldn’t have it (otherwise it would be executable):
rw-r--r--
How can you set it up for all nested files/folders in your application folder?
You can solve this task by setting common persmissions for all files and folders (step 1) and then make folders accessable (step 2), recursively:
chmod -R 644 .
chmod -R a+X .
But chmod allows you to set up only access flag for folders, you cannot set one persmission for folders and another for files.
Here is a script written by my collegue Anton [vojtoshik] Voitovych to solve this task.
Copy it and save to a file (I saved it in /bin/rchmod — thus it’s accessable for all users of my system):
#!/bin/bash
files="0644";
directories="0755";
path=$1;
shift;
while getopts "d:f:" OPTION
do
case $OPTION in
f) files="$OPTARG" ;;
d) directories="$OPTARG" ;;
esac
done
if [ "$path" != '' ]; then
find "$path" -type d ! -name "." -exec chmod "$directories" {} \;
find "$path" -type f -exec chmod "$files" {} \;
else
echo "Usage: rchmod <path_to_directory> [-d <directories mode>] [-f <files mode>]";
echo -ne \\n;
fi
OK, now make it executable:
chmod 755 rchmod
How to use it.
1. You are in your application folder and want to set 755 (drwxr-xr-x) mode for folders and 644 (rw-r--r--) mode for files which is the default:
rchmod .
2. You want to set 123 permissions only for folders in a particular folder::
rchmod /some/path/to/folder -d 123
3. You want to set 123 permissions for folders and 456 permission for files in a particular folder::
rchmod /some/path/to/folder -d 123 -f 456
Note: don’t forget that every time after that command run you should make some folders writable (wp-content/uploads, cache, temp — what else you have in your webapp).
July 9th, 2009 at 21:07
на хуя так сложно ))
find . -type d | xargs chmod a+x
find . -type f | xargs chmod a+w