Skip to content

Instantly share code, notes, and snippets.

@vmiller
Created November 8, 2016 16:27
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save vmiller/fc761c2bb019c9c1cf71d5194c235760 to your computer and use it in GitHub Desktop.
Save vmiller/fc761c2bb019c9c1cf71d5194c235760 to your computer and use it in GitHub Desktop.
Script to delete user account profiles
#!/bin/bash
#
# Script to delete user profile directories excluding a list of known
# users to keep. Must be run as root, and is intended to run as a launch
# daemon
#
# define users to keep in an array
# (use spaces between items)
KEEPERS=( ladmin )
AGE=7 # Number in days for age of accounts
# iterate through list of folders in /Users
for folder in /Users/*; do
# remove the "/Users/" portion of the path for easier testing
user=$(basename ${folder})
# compare folder name against KEEPER array items
if [[ "${KEEPERS[*]}" =~ "${user}" ]]; then
# skip if folder is in the skip array
logger -t $0 "Skipping ${user}"
else
# Check age of account and proceed with removal if appropriate
logger -t $0 "Checking age of ${user}..."
if [[ -n $(find /Users/${user} -maxdepth 0 -type d -mtime +${AGE}d) ]]; then
logger -t $0 "Deleting ${user}"
rm -rf /Users/${user}
else
logger -t $0 "Keeping ${user}"
fi
fi
done
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment