Tag Archives: delete

Removing ‘other’ Users Home Folders – in a mobile home folder environment

OS X Home FolderIf you have a large network that uses mobile home folders which allows multiple users to access any given machine, but each machine is assigned a primary user then this little addition to your login hook might be for you. Let me clarify when I say ‘Allow multiple users access’, I mean they can authenticate to a machine but normally DO NOT work on it. The ‘Primary User’ is the person who is expected to be logged into this machine the majority of the time, but say another user need to check their e-mail or get a document, or something where they need access for a limited time – then this little bit of code will remove any of their personal files when the ‘Primary User’ logs back in.

# Author: Joseph J. Viscomi    E-Mail: jjviscomi [at] gmail [dot] com || jviscomi [at] brehm [dot] org
# Date: 3/11/2011
# Description: This script can be included in a login hook when you want to remove all mobile or other home folders that
#              do not belong to the primary user of this computer. Mainly used to remove unwanted files and free disk space.
#              Once this script is applied in order for it to work correctly the first person that logs in must be the
#              'Primary User' of the computer.
#
#		Every time the 'Primary User' signs on to the computer it will remove all other home folders.
#
#              If you want to reset the 'Primary User' run the command - sudo rm -f /var/log/currentUser.log
#              then restart the computer and login as the 'Primary User'

#REMOVE HOME FOLDERS FOR NON-PRIMARY USERS
#CHECK TO MAKE SURE THE USER LOGGING IN HAS A HOME FOLDER
if [ -d "/Users/$1" ]; then
	#CHECKS TO SEE IF THE FILE THAT CONTAINS THE 'PRIMARY USER' EXISTS
	if [ -e "/var/log/currentUser.log" ]; then
		#CHECK TO SEE IF THE CURRENT USER IS THE 'PRIMARY USER'
		if [ `cat /var/log/currentUser.log` = $1 ]; then
                        #REMOVE ALL OTHER USER HOME FOLDERS - NOTE: DO NOT REMOVE Shared FOLDER, THIS CAN CAUSE PROBLEMS!
			#LOG THE OPERATIONS TO SYSLOG
			ls  /Users/ | grep -v `cat /var/log/currentUser.log` | grep -v Shared | xargs -I {} sudo rm -vrf /Users/{} >> /var/log/system.log
		fi
	else
		#IF THE FILE WHICH CONTAINS THE CURRENT USER DOESN'T EXISTS THEN THE PERSON THAT IS LOGGIN IN MUST THE 'PRIMARY USER'
		#RECORD THEIR USER NAME IN THE LOG FILE FOR FUTURE USE.
		sudo echo $1 > /var/log/currentUser.log
	fi
fi

The above script has been simplified for easy readability and commented to improve understanding of what is going on.

Performance Optimization WordPress Plugins by W3 EDGE