We have been needing to find a script that works to delete all the home folders in the Users directory except for four that we need to keep (map, tech, student, and admn). We have a setup of mobile home directories that are cached on each laptop when network users first use them. Here is the script that support helped build. I am getting a syntax error when running it at the first "for".
#!/bin/sh
########################################################################################################
#
# Copyright (c) 2013, JAMF Software, LLC. All rights reserved.
#
# Redistribution and use in source and binary forms, with or without
# modification, are permitted provided that the following conditions are met:
# * Redistributions of source code must retain the above copyright
# notice, this list of conditions and the following disclaimer.
# * Redistributions in binary form must reproduce the above copyright
# notice, this list of conditions and the following disclaimer in the
# documentation and/or other materials provided with the distribution.
# * Neither the name of the JAMF Software, LLC nor the
# names of its contributors may be used to endorse or promote products
# derived from this software without specific prior written permission.
#
# THIS SOFTWARE IS PROVIDED BY JAMF SOFTWARE, LLC "AS IS" AND ANY
# EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
# WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
# DISCLAIMED. IN NO EVENT SHALL JAMF SOFTWARE, LLC BE LIABLE FOR ANY
# DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
# (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
# LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
# ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
# (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
# SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
#
####################################################################################################
# PURPOSE
# - Remove or Move local user directories from AD bound machines that have "Force Local Homes" checked
#
# HISTORY
# Version: 1.0
# - Created by Bram Cohen October 15. 2013
#
####################################################################################################
username1="map"
username2="tech"
username3="student"
username4="admn"
####################################################################################################
# SCRIPT OPERATIONS - - REALLY!!! - DO NOT MODIFY BELOW THIS LINE
####################################################################################################
RESULT=""
for U in /Users/*; do
if [ -d "$U" ]; then
if [ "$U" == "/Users/Shared" ] || [ "$U" == "/Users/Guest" ]; then
/bin/echo "Found $U, ignored"
USERNAME=`/bin/echo $U | tr '/' ' ' | awk '{print $NF}'`
RESULT=`echo "$RESULT$USERNAME-IGNORED "`
else
/bin/echo "Found $U, continuing..."
USERNAME=`/bin/echo $U | tr '/' ' ' | awk '{print $NF}'`
/bin/echo "Parsed username as: $USERNAME"
ADMINCHECK=`/usr/bin/dsmemberutil checkmembership -U $USERNAME -G admin | awk '{print $3}'`
if [ "$ADMINCHECK" == "not" ]; then
if [ "$USERNAME" == "$username1" ] || [ "$USERNAME" == "$username2" ] || [ "$USERNAME" == "$username3" ] || [ "$USERNAME" == "$username4" ]; then
/bin/echo "$USERNAME is on the exempt list, ignoring..."
RESULT=`echo "$RESULT$USERNAME-EXEMPT "`
else
/bin/echo "$USERNAME not an Admin nor exempt, taking action..."
RESULT=`echo "$RESULT$USERNAME-Action Taken "`
#/bin/echo "Forcing the removal of $U" && /bin/rm -rf $U
fi
else
/bin/echo "$USERNAME was found to be a Local Admin, ignoring"
RESULT=`echo "$RESULT$USERNAME-ADMIN "`
fi
fi
fi
done
echo ""
echo "============================================================"
echo "Summary: $RESULT"
echo "============================================================"