Posted By

cruelfate on 09/19/12


Tagged

permissions


Versions (?)

Who likes this?

1 person have marked this snippet as a favorite

thescorpion


fix_perm.sh


 / 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.

  1. #! /bin/bash
  2.  
  3. DIR_MODE="775"
  4. FILE_MODE="g+w"
  5. GROUP=myconfig
  6. PATTERN='^/etc/myconfig/'
  7.  
  8. sudo=
  9. id="$(whoami)"
  10. if [ "root" != "$id" ] ; then
  11. sudo="sudo -E"
  12. fi
  13.  
  14. targets=.
  15. if [ $# -gt 0 ] ; then
  16. targets="$*"
  17. fi
  18.  
  19. for target in $targets; do
  20. if [ ! -r $target ] ; then
  21. echo "ERROR: Path '"$target"' is not readable."
  22. exit 1;
  23. fi
  24. target=$(cd "$(dirname $target)"; pwd)/$(basename $target)
  25. # remove double slash in case of top-level directory
  26. target=${target/\/\//\/}
  27. if echo $target | grep -o -E $PATTERN ; then
  28. if [ ! -e $target ] ; then
  29. echo "ERROR: Path '"$target"' does not exist."
  30. exit 1;
  31. fi
  32. echo "Fixing configuration permissions of '"$target"'"
  33. $sudo chgrp -R $GROUP $target
  34. $sudo chmod $DIR_MODE $(find $target -type d)
  35. $sudo chmod $FILE_MODE $(find $target -type f)
  36. else
  37. echo "I'm sorry, $id. I'm afraid I can't do that to '"$target"'."
  38. exit 1;
  39. fi
  40. done

Report this snippet  

You need to login to post a comment.