Quote:
Originally Posted by ramesh14
I have a directory which contains some directories and files. and the sub-dirs contains some more dirs and files. All the directories and files inside the main directory are having different permissions. I want to give 0755 for all dirs and 0644 for all files in the main dir. Changing permissions for each and every thing is a critical job. This bash shell script if for a single dir or file (example: polo).
Code:
if [ -d polo ]
chmod 0755 polo;
elif [ -f polo ]
chmod 0644 polo;
else
echo " polo is not a dir or file";
Further i am not understanding. I have to perform this action many time when ever i copy contents. Can any body please help me?
|
This should work. Just substitute /path/to/ by the correct path, and all the files and dirs under that node will be processed.
Code:
find /path/to/ \( -type d -exec echo chmod 0755 '{}' \; -o -type f -exec chmod 0644 '{}' \; \)
If you want, you could as well do a bash function, for example, try putting this into your ~/.bashrc and ~/.bash_profile files:
Code:
# Function to fix permissions
function fixperms () {
if [ -z "$1" ]
then
echo "Usage: fixperms base_directory [file permissions] [dir_permissions]"
return
fi
if [ -z "$2" ]; then local FILE_PERMS=0644; else FILE_PERMS="$2"; fi
if [ -z "$3" ]; then local DIR_PERMS=0755; else DIR_PERMS="$3"; fi
echo "* Using $FILE_PERMS for files and $DIR_PERMS for directories."
if [ -d "$1" ] || [ -f "$1" ]
then
find "$1" \( \
-type d -exec chmod "$DIR_PERMS" '{}' \; -o \
-type f -exec chmod "$FILE_PERMS" '{}' \; \
\)
else
echo "! $1 not found."
fi
}
This will be quite a time saver if you do this a lot. Now you can reload your shell (just close your terminal and open a new one, run exec bash or re-login if you wish) so the changes takes effect. From now on, you should be able to just run
Code:
$ fixperms /any/dir/you/want
Probably much easy to handle than the sentence I wrote above.
EDIT: I found an improved version of this function on my home and pasted it instead. It's basically the same with a couple of checks and basic help.