Advertisement
Guest User

Untitled

a guest
Oct 16th, 2019
104
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.53 KB | None | 0 0
  1. #!/usr/bin/env bash
  2.  
  3. # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # #
  4. # MIT License
  5. #
  6. # Copyright (c) 2019 Björn Richter
  7. #
  8. # Permission is hereby granted, free of charge, to any person obtaining a copy
  9. # of this software and associated documentation files (the "Software"), to deal
  10. # in the Software without restriction, including without limitation the rights
  11. # to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  12. # copies of the Software, and to permit persons to whom the Software is
  13. # furnished to do so, subject to the following conditions:
  14. #
  15. # The above copyright notice and this permission notice shall be included in all
  16. # copies or substantial portions of the Software.
  17. #
  18. # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  19. # IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  20. # FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  21. # AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  22. # LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  23. # OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
  24. # SOFTWARE.
  25. # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # #
  26.  
  27. USAGE="
  28. Usage:
  29. ./fix-permissions.sh <OWNER|:GROUP|OWNER:GROUP|-> <DIR_MODE|-> <FILE_MODE|-> PATH...
  30.  
  31. Examples:
  32. Fix permissions for home directory
  33.  
  34. ./fix-permissions.sh tony:stark 755 644 /home/tony/
  35.  
  36. Fix permissions for specific directories and files and only change user, not group:
  37.  
  38. ./fix-permissions.sh tony 700 600 /home/tony/.ssh/ /home/tony/.my.cnf
  39.  
  40. Fix only file permissions and change only group (for folders as well as files)
  41.  
  42. ./fix-permissions.sh :stark 755 - /srv/data/tony/
  43.  
  44. Any of the parameters in angle brackets \`<...>\` can be skipped by passing a
  45. single dash \`-\`. For example, when passing a \`-\` for DIR_MODE, the dir mode
  46. will not be changed.
  47. "
  48.  
  49. set -e # fail when command failed
  50.  
  51. if [[ $1 =~ ^(-h|--help)$ ]]; then
  52. echo "$USAGE"
  53. exit
  54. fi
  55.  
  56. if [[ $# -lt 4 ]]; then
  57. echo >&2 "$USAGE"
  58. exit 1
  59. fi
  60.  
  61. owner_group="$1"
  62. dir_mode="$2"
  63. file_mode="$3"
  64. paths="${@:4}"
  65.  
  66. # Fix owner / group
  67. if [[ $owner_group != '-' ]]; then
  68. chown -R "$owner_group" "${paths[@]}"
  69. fi
  70.  
  71. # Fix directry permissions
  72. if [[ $dir_mode != '-' ]]; then
  73. find "${paths[@]}" -type d -exec chmod "$dir_mode" "{}" +
  74. fi
  75.  
  76. # Fix file permissions
  77. if [[ $file_mode != '-' ]]; then
  78. find "${paths[@]}" -type f -exec chmod "$file_mode" "{}" +
  79. fi
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement