-
Notifications
You must be signed in to change notification settings - Fork 0
/
purgeOldUsers.sh
94 lines (75 loc) · 1.93 KB
/
purgeOldUsers.sh
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
#!/bin/bash
# Modified 2015-03-11
# delete_inactive_users.sh
# Maintained at https://github.com/dankeller/macscripts
# by Dan Keller
#
# MIT License
#
# Modified by Daniel Engh
# Nov 8, 2017
#======================================
#
# Script to delete local user data that has not been accessed in a given time
# period.
#
# This script scans the /Users folder for the date last updated (logged in)
# and deletes the folder as well as the corresponding user account if it has
# been longer than the time specified. You can specify user folders to keep as
# well.
#
# User data not stored in /Users is not effected.
#
# Helpful for maintaing shared/lab Macs connected to an AD/OD/LDAP server.
#
#======================================
#----Variables----
# DEFAULT VALUE FOR "AGE" IS SET HERE
AGE=122 # Delete /Users/ folders inactive longer than this many days
# CHECK TO SEE IF A VALUE WAS PASSED IN PARAMETER 4 AND, IF SO, ASSIGN TO "AGE"
if [ "$4" != "" ]; then
AGE=$4
fi
# User folders you would like to bypass. Typically local users or admin accounts.
KEEP=("/Users/Shared")
if [ "$5" != "" ]; then
KEEP+=("/Users/"$5)
fi
if [ "$6" != "" ]; then
KEEP+=("/Users/"$6)
fi
if [ "$7" != "" ]; then
KEEP+=("/Users/"$7)
fi
if [ "$8" != "" ]; then
KEEP+=("/Users/"$8)
fi
if [ "$9" != "" ]; then
KEEP+=("/Users/"$9)
fi
if [ "${10}" != "" ]; then
KEEP+=("/Users/"${10})
fi
if [ "${11}" != "" ]; then
KEEP+=("/Users/"${11})
fi
#--End variables--
### Delete Inactive Users ###
if [[ ${UID} -ne 0 ]]; then
echo "$0 must be run as root."
exit 1
fi
USERLIST=$(/usr/bin/find /Users -type d -maxdepth 1 -mindepth 1 -not -name "." -mtime +"${AGE}")
echo "Performing inactive user cleanup"
for a in ${USERLIST}; do
if ! [[ ${KEEP[*]} =~ "$a" ]]; then
echo "Deleting inactive (over ${AGE} days) home directory: $a"
# delete home folder
/bin/rm -r "$a"
continue
else
echo "SKIPPING $a"
fi
done
echo "Cleanup complete"
exit 0