/ Published in: Bash
Recursively set file and directory modes and group to specified file mode, directory mode and group ID.
TODO: specify modes and group as command line options.
Expand |
Embed | Plain Text
#! /bin/bash DIR_MODE="775" FILE_MODE="g+w" GROUP=myconfig PATTERN='^/etc/myconfig/' sudo= id="$(whoami)" if [ "root" != "$id" ] ; then sudo="sudo -E" fi targets=. if [ $# -gt 0 ] ; then targets="$*" fi for target in $targets; do if [ ! -r $target ] ; then echo "ERROR: Path '"$target"' is not readable." exit 1; fi target=$(cd "$(dirname $target)"; pwd)/$(basename $target) # remove double slash in case of top-level directory target=${target/\/\//\/} if echo $target | grep -o -E $PATTERN ; then if [ ! -e $target ] ; then echo "ERROR: Path '"$target"' does not exist." exit 1; fi echo "Fixing configuration permissions of '"$target"'" $sudo chgrp -R $GROUP $target $sudo chmod $DIR_MODE $(find $target -type d) $sudo chmod $FILE_MODE $(find $target -type f) else echo "I'm sorry, $id. I'm afraid I can't do that to '"$target"'." exit 1; fi done
You need to login to post a comment.
