my xfce4 dotfiles
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

82 lines
2.5 KiB

3 years ago
  1. # Some useful commands for setting permissions.
  2. #
  3. # Rory Hardy [GneatGeek]
  4. # Andrew Janke [apjanke]
  5. ### Aliases
  6. # Set all files' permissions to 644 recursively in a directory
  7. set644() {
  8. find "${@:-.}" -type f ! -perm 644 -print0 | xargs -0 chmod 644
  9. }
  10. # Set all directories' permissions to 755 recursively in a directory
  11. set755() {
  12. find "${@:-.}" -type d ! -perm 755 -print0 | xargs -0 chmod 755
  13. }
  14. ### Functions
  15. # fixperms - fix permissions on files and directories, with confirmation
  16. # Returns 0 on success, nonzero if any errors occurred
  17. fixperms () {
  18. local opts confirm target exit_status chmod_opts use_slow_mode
  19. zparseopts -E -D -a opts -help -slow v+=chmod_opts
  20. if [[ $# > 1 || -n "${opts[(r)--help]}" ]]; then
  21. cat <<EOF
  22. Usage: fixperms [-v] [--help] [--slow] [target]
  23. target is the file or directory to change permissions on. If omitted,
  24. the current directory is taken to be the target.
  25. -v enables verbose output (may be supplied multiple times)
  26. --slow will use a slower but more robust mode, which is effective if
  27. directories themselves have permissions that forbid you from
  28. traversing them.
  29. EOF
  30. exit_status=$(( $# > 1 ))
  31. return $exit_status
  32. fi
  33. if [[ $# == 0 ]]; then
  34. target="."
  35. else
  36. target="$1"
  37. fi
  38. if [[ -n ${opts[(r)--slow]} ]]; then use_slow=true; else use_slow=false; fi
  39. # Because this requires confirmation, bail in noninteractive shells
  40. if [[ ! -o interactive ]]; then
  41. echo "fixperms: cannot run in noninteractive shell"
  42. return 1
  43. fi
  44. echo "Fixing perms on $target?"
  45. printf '%s' "Proceed? (y|n) "
  46. read confirm
  47. if [[ "$confirm" != y ]]; then
  48. # User aborted
  49. return 1
  50. fi
  51. # This xargs form is faster than -exec chmod <N> {} \; but will encounter
  52. # issues if the directories themselves have permissions such that you can't
  53. # recurse in to them. If that happens, just rerun this a few times.
  54. exit_status=0;
  55. if [[ $use_slow == true ]]; then
  56. # Process directories first so non-traversable ones are fixed as we go
  57. find "$target" -type d ! -perm 755 -exec chmod $chmod_opts 755 {} \;
  58. if [[ $? != 0 ]]; then exit_status=$?; fi
  59. find "$target" -type f ! -perm 644 -exec chmod $chmod_opts 644 {} \;
  60. if [[ $? != 0 ]]; then exit_status=$?; fi
  61. else
  62. find "$target" -type d ! -perm 755 -print0 | xargs -0 chmod $chmod_opts 755
  63. if [[ $? != 0 ]]; then exit_status=$?; fi
  64. find "$target" -type f ! -perm 644 -print0 | xargs -0 chmod $chmod_opts 644
  65. if [[ $? != 0 ]]; then exit_status=$?; fi
  66. fi
  67. echo "Complete"
  68. return $exit_status
  69. }