#!/bin/bash # # *** USE AT YOUR OWN RISK! *** # # License: http://creativecommons.org/licenses/by/3.0/us/ usage() { echo "Usage: ${0##*/} -u uid do not alter users below this uid -g gid do not alter groups below this gid -p path start at this path " 2>&1 exit $1 } startingUid="" startingGid="" startingPath="" while getopts "u:g:p:" opt do case "$opt" in u) startingUid="$OPTARG";; g) startingGid="$OPTARG";; p) startingPath="$OPTARG";; [?]) usage 1;; esac done if [[ -z "$startingUid" ]] || [[ -z "$startingGid" ]] || [[ -z "$startingPath" ]] then usage 1 fi echo '#!/bin/bash' echo '[[ "$USER" != "root" ]] && ( echo "must be root"; exit 1 )' # fix users awk -F: '{print $1, $3, $6}' /etc/passwd | \ while read user uid home do if [[ $uid -ge $startingUid ]] then echo "[[ -d \"$home\" ]] && chown $user $home" echo "find $startingPath -nouser -uid $uid -exec chown $user {} \;" fi done # fix groups awk -F: '{print $1, $3}' /etc/group | \ while read group gid do if [[ $gid -ge $startingGid ]] then echo "find $startingPath -nogroup -gid $gid -exec chgrp $group {} \;" fi done