#!/usr/bin/env bash function usage () { if [ -n "$1" ];then echo -e "\nError: $1" fi cat< [-g groupname] `basename $0` proofs ownership of the given pathname, and change it to the given username if needed. If flag "-g" is given, also group membership will be changed. If pathname is a directory, all regular files inside will be proofed and also changed if needed. If flag "-r" is present, also subdirectories will be considered. Mandytory Options: -u username name of the user, that should own the given file(s) Optinal Control Options: -h print that help text -g groupname name of the group, the given file(s) should be member of. -r means recursive. script also traverses into subdirectories Example: /root/bin/check_ownership.sh -u amavis -g amavis /var/lib/amavis/.spamassassin EOF exit } recursiv=false while getopts d:g:hru: opt ; do case $opt in h) usage ;; u) user="$OPTARG" ;; g) group="$OPTARG" ;; r) recursiv=true ;; \?) usage ;; esac done shift `expr $OPTIND - 1` path=$1 if [ -z "$path" ];then usage "Es wurde kein Verzeichnis angegeben" fi if [ ! -d "$path" -a ! -f "$path" ];then usage "\"$path\" is neither a directory nor a regular File" fi if [ -z "$user" ];then usage "Benutzername (-u) muss angegeben werden" fi [ $# -ne "1" ] && usage "wrong number of arguments" if [ -z "$group" ] ; then _ownership=$user check_group=false else _ownership="$user:$group" check_group=true fi function check_owner () { _path=$1 change_ownership=false if [ -d $_path -o -f $_path ]; then _u=`stat -c "%U" $_path` _o="$_u" if [ "$_u" != "$user" ]; then change_ownership=true fi if $check_group ; then _g=`stat -c "%G" $_path` _o="${_u}:$_g" if [ "$_g" != "$group" ]; then change_ownership=true fi fi if $change_ownership ; then echo echo "$_path has wrong ownership ($_o)." echo -en "\tChange to ownership $_ownership.." chown $_ownership $_path > /dev/null 2>&1 if [ "$?" = "0" ];then echo " [ Ok ]" else echo " [ Failed ]" fi echo fi if [ -d $_path ]; then for _file in $_path/* ; do if [ -d $_file ] && $recursiv ; then check_owner $_file elif [ -f $_file ]; then check_owner $_file fi done fi fi } check_owner $path exit 0