#!/bin/bash function fix_user_home_dir_owner_perms () { local LINE FOUND UN HD for LINE in $( sed -n "\@\(`sed -n '\#^/#p' /etc/shells | sed '\#nologin#d' | sed -e :a -e 'N;s/\n/\\\\|/g;ta'`\)@p" /etc/passwd | sed 's/^\([^:]*\):[^\/]*\(\/[^:]*\):\(.*\)$/UN=\1;HD=\2;/g' ); do # set to blank FOUND= UN= HD=; # eval the LINE read from /etc/passwd - UN=username;HD=/home/userhome; eval $LINE; echo "DEBUG: USERNAME:${UN} HOMEDIRECTORY: ${HD}" # search users home dir for anything they arent the user:group owner of, if any found print detailed info about it and save to the FOUND var FOUND=$( find $HD -mount \( ! -user $UN -o ! -group $UN \) -printf '%.5m %10M %#9u:%-9g %#5U:%-5G [%AD | %TD | %CD] [%Y] %p\n' 2>/dev/null ); # if results, echo USER - HOME followed by all the results saved in FOUND, then recursively chown home dir of user, showing changes [[ ${#FOUND} -gt 1 ]] && echo -e "\n\n --- ${UN}:${UN} - ${HD}\n${FOUND}" #&& chown --preserve-root -cR $UN:$UN $HD done } # this runs the function declared above fix_user_home_dir_owner_perms # exit with success or failure exit $?