#!/usr/bin/env bash # Smartly change permissions on selected directories # Copyright 2021 Bryan C. Roessler [[ -f functions ]] && . functions || exit 1 is_root if [[ $# -eq 0 ]]; then user=$(stat -c "%U" "$path") group=$(stat -c "%G" "$path") echo "No arguments provided, using autodetection" if [[ "$group" != "smbgrp" ]]; then echo "This file will not be world-accessible by the smbgrp group" ask_ok "Change group $group to smbgrp?" && group="smbgrp" fi elif [[ $# -eq 1 ]]; then user="$1" group="$1" paths=("$PWD") elif [[ $# -eq 2 ]]; then user="$1" group="$2" paths=("$PWD") elif [[ $# -gt 2 ]]; then user="$1" group="$2" paths=("${@:3}") fi for path in "${paths[@]}"; do if [[ "$path" == "/" ]]; then echo "You are trying to operate on the root partition!" echo "This seems highly unusual!" ! ask_ok "Continue?" && exit $? fi done ask_ok "Apply user: $user and group: $group to ${paths[*]} and all subdirs?" && \ chown -R "$user":"$group" "${paths[@]}" [[ "$group" == "smbgrp" ]] && mode=6775 || mode=755 ask_ok "Apply chmod $mode to ${paths[*]} and all subdirs?" && \ chmod -R $mode "${paths[@]}" # Let's do it in less steps (see above) for now unless it becomes a problem # echo "Apply setuid/setgid bits to ${paths[*]} and all subdirs?" # ask_ok "Files/dirs will inherit their " && \ # chmod -R g+s,u+s "${paths[@]}" exit $?