Skip to content

Instantly share code, notes, and snippets.

@rickychilcott
Created August 7, 2012 12:50
Show Gist options
  • Save rickychilcott/3285060 to your computer and use it in GitHub Desktop.
Save rickychilcott/3285060 to your computer and use it in GitHub Desktop.
Remove Unused Accounts (with keepable list)
#!/bin/bash
# List of users to keep
KEEP=$( cat <<EOL
/Users/admin
/Users/sccadmin
/Users/mdiaadmin
/Users/Shared
/Users/Search
EOL);
# Number of days to keep
DAYS=30
USERLIST=`find /Users -type d -maxdepth 1 -mindepth 1 -not -name "*.*" -mtime +$DAYS`
for user in $USERLIST ; do
# Keep select accounts
for keep in $KEEP ; do
[[ "$user" == "$keep" ]] && user="" && continue
done
# Remove user's home directory and DSCL entry
if [[ "$user" != "" ]]; then
echo "Deleting account and home directory for" $user
dscl . delete $user #delete the account
rm -r $user #delete the home directory
fi
done
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment