Submit Hint Search The Forums LinksStatsPollsHeadlinesRSS
14,000 hints and counting!


Click here to return to the 'A bit more thorough...' hint
The following comments are owned by whoever posted them. This site is not responsible for what they say.
A bit more thorough...
Authored by: gatorparrots on Jun 12, '03 01:05:02PM
#!/bin/sh

##
# rmuser
# This script will remove user accounts on OS X 10.2.x
# author: gatorparrots 
# problems or suggestions can be posted to the macfora.com OS X UNIX forum
#
# usage: rmuser [username]
##

if [ $# -eq 0 ]; then
  printf "usage: rmuser [username]\n"
  printf "User account and user's homedir will be irretrievably removed.\n" >&2
  exit 2
fi

username=$1

#safety checks
if [ "$username" = "root" ]; then
  echo "That's not a wise thing to do."
  exit 1
fi
if [ -z "`niutil -read . /users/$username 2> /dev/null `" ]; then
	echo "$username does not exit."
	exit 2
fi

# Need to be a sudoer to remove user
sudo -p "Please authenticate to remove user (administrator password): " printf "" || {
	echo "Abort: could not authenticate" >&2
	exit 1
}

userID=$(niutil -readprop . /users/$username uid)

#remove configuration files and other vestiges
if sudo [ -f /var/db/samba/hash/$username ]; then
    	sudo rm -f /var/db/samba/hash/$username
fi
if sudo [ -f "/etc/httpd/users/$username.conf" ]; then
		sudo rm -f "/etc/httpd/users/$username.conf"
fi
if [ -f "/Users/Deleted Users/$username.dmg" ]; then
		sudo rm -f "/Users/Deleted Users/$username.dmg"
fi
if sudo [ -f "/Library/Application Support/SyncService/$userID" ]; then
    	sudo rm -rf "/Library/Application Support/SyncService/$userID"
fi
if sudo [ -d /private/tmp/$userID ]; then
    	sudo rm -rf /private/tmp/$userID
fi
sudo rm -rf /.Trashes/$userID
sudo rm -rf /Volumes/*/.Trashes/$userID
sudo rm -f /Library/caches/*.$username
sudo rm -f /Library/caches/*.$userID
sudo rm -f "/Library/caches/Desktop Pictures/$username.*"

##You may wish to retain the user's homedir; if so, comment lines below 
userhome="`niutil -readprop . /users/$username home`"
sudo rm -rf $userhome

#remove admin status, if exists
sudo niutil -destroyval . /groups/admin users $username

#remove user
sudo niutil -destroy . /users/$username
sudo niutil -resync .

#reassign file ownership to main owner (501)
#comment out or change reassignment UID, if desired
sudo find / -user $userID -exec chown 501 {} \;

echo 'User account "'$username'" removed.'


[ Reply to This | # ]
GeekLog ate the backslash...
Authored by: gatorparrots on Jun 12, '03 01:16:17PM
Near the end of the script, the UID reassignment code should be: sudo find / -user $userID -exec chown 501 {} \;

[ Reply to This | # ]
GeekLog ate the backslash II
Authored by: gatorparrots on Jun 12, '03 01:17:50PM
sudo find / -user $userID -exec chown 501 {} \;

[ Reply to This | # ]