A small guide to restore default file permissions and owner on CMSMS filesystem
Posted: Sat Mar 29, 2008 12:22 am
Sometimes happens, while uploading new files or creating new directory in CMSMS system dir, to modify unintentionally file/directory permissions/owner.
When you manually try to restore file permissions, it's very boring to restore different permissions for directories and files, especially when number of files to change is huge.
My first approach to solve this problem was to chmod files recursively, for example on directory /uploads I used:
# chmod 755 * -R
but this command chmods everything, files and directories with the same permission (755) without distinction. That was not a good solution.
Digging in google I've found a nice bash script, handful to restore all permissions to default settings.
By default It restores 640 to files and 750 to directory but you can set your own permissions, passing them as argument to the script.
The script name is chmodr. Here following the code. In green a (bad) translation.
***
***
Finally, to restore default owner of files and directories just open a shell and write (if owner is www-data)
chmod www-data:www-data * -R
That's all
Feedback are welcome.
Regards
blast
When you manually try to restore file permissions, it's very boring to restore different permissions for directories and files, especially when number of files to change is huge.
My first approach to solve this problem was to chmod files recursively, for example on directory /uploads I used:
# chmod 755 * -R
but this command chmods everything, files and directories with the same permission (755) without distinction. That was not a good solution.
Digging in google I've found a nice bash script, handful to restore all permissions to default settings.
By default It restores 640 to files and 750 to directory but you can set your own permissions, passing them as argument to the script.
The script name is chmodr. Here following the code. In green a (bad) translation.
***
Code: Select all
#!/bin/bash
#
# chmodr - Un chmod Ricorsivo, che rispetta le differenze file-directory
# [color=green]chmodr - a recursive chmod that respects difference between file-directory[/color]
# Copyright (C) 2004, 2006 - Antonio Ingargiola <debian@fastwebnet.it>
#
# Released under the GNU GENERAL PUBLIC LICENSE.
#
help () {
cat << EOF
chmodr - Un chmod Ricorsivo, che rispetta le differenze file-directory
Copyright (C) 2004, 2006 - Antonio Ingargiola <debian@fastwebnet.it>
Released under the GNU GENERAL PUBLIC LICENSE.
DESCRIZIONE [color=green]DESCRIPTION[/color]
Cambia ricorsivamente i permessi di file e directory, ma impostando
permessi diversi per i file e per le directory. Nella ricorsione non
segue link simbolici a directory.
[color=green]This script allow to change recursively permissions of files and directories,
using different permissions for files and for directories. it doesn't follow symlink to directory.[/color]
USO [color=green]USAGE[/color]
chmodr
Senza parametri, partendo dalla directory corrente imposta i permessi
640 (- rw- r-- ---) a tutti i file e 750 (- rwx r-x ---) a tutte le
sotto-directory.
[color=green] Without parameters, it starts from current directory and sets to all files
640 (- rw- r-- ---) permission and 750 (- rwx r-x ---) to all subdirectories[/color]
chmodr permessi_file permessi_dir
Applica ai file nella directory corrente e sotto-directory i
'permessi_file' e tutte le directory i 'permessi_dir'. La sintassi
per i permessi e' la stessa di chmod quindi sia ottale (es. 640) che
simbolica (es. o-rwx).
[color=green]chmodr file_permission directory_permission
Apply to all files in current dir and to all subdir all file_permission
directory_permission. Syntax is the same of chmod, both octal (eg. 640)
than symbolic (eg. o-rwx).[/color]
E' possibile specificare un terzo parametro opzionale che indica una
directory iniziale diversa da quella corrente.
[color=green]As third argument is possible to choose a different start directory, not current one.[/color]
EOF
exit 1
}
valid_mod () {
# Controlla che i permessi siano sintatticamente corretti
echo "$1" |\
perl -n -e 'if ($_ =~ /^[01234567]{3}$/) {exit 0}; exit 1'
ok_numerical=$?
echo "$1" |\
perl -n -e 'if ($_ =~ /^[ugoa]{0,3}[+-=]{1}[rwx]{1,3}$/) {exit 0}; exit 1'
ok_symbolical=$?
# echo "num $ok_numerical, sym $ok_symbolical" # DEBUG
[ "$ok_numerical" = 0 -o "$ok_symbolical" = 0 ]
return $?
}
[[ "$1" = -* ]] && help
FILE_MOD="$1"
DIR_MOD="$2"
BASE_DIR="$3"
[ -z "$1" ] && FILE_MOD="640"
[ -z "$2" ] && DIR_MOD="750"
[ -z "$3" ] && BASE_DIR="./"
cd "$BASE_DIR"
if !( valid_mod $FILE_MOD && valid_mod $DIR_MOD ); then
echo -e "\n ERRORE: Il formato dei permessi e' errato. Per i dettagli"
echo -e " vedere la pagina di manuale di chmod.\n"
exit 2
fi
ls | while read file; do
if [ -L "$file" ]; then
echo " ==>> '$file' e' un link simbolico, lo ignoro."
continue
elif [ -d "$file" ]; then
echo " Entro nella directory '$file'"
chmod u+rwx "$file" 2> /dev/null ||\
{ echo " Non ho i permessi per entrare in '$file'"; continue; }
cd "$file"; $0 $@; cd - > /dev/null
chmod $DIR_MOD "$file" && echo -e " Directory '$file' impostata.\n"
elif [ -f "$file" ]; then
echo -n " $file ... "
chmod $FILE_MOD "$file" && echo " [ OK ]"
else
echo " Ignoro '$file'."
fi
done
Finally, to restore default owner of files and directories just open a shell and write (if owner is www-data)
chmod www-data:www-data * -R
That's all
Feedback are welcome.
Regards
blast