Working in a school, I often need to clean up the old user folders in /Users on our lab machines. We don’t “freeze” our labs for various reasons, but mostly so the computer behaves as the user expects and they don’t lose the project they were working on simply because they saved it to the computer instead of their flash drive. Over time, the computer accumulates a lot of home folders!
I sure don’t want to delete them manually.
A (dangerous) solution is to script the deletion of the folders. I say it is dangerous because any time you are deleting files with a script, you run the risk of lost data. You have been warned.
I needed the script to only delete folders that had not been accessed in a while. The idea is to delete folders from users who had not logged in at all the current trimester. 90 days should do.
I also needed to keep some local users, an admin account, a special user set up for online testing and a student guest account that gets reset after each reboot.
I used an array to list the folders I don’t want to touch.
KEEP=("/Users/Shared" "/Users/support" "/Users/student" "/Users/testing")
find with a simple date check on the folder to determine if it should be deleted. Each time a user logs in, it updates files in the folder, therefore updating it’s timestamp. Great!
USERLIST='/usr/bin/find /Users -type d -maxdepth 1 -mindepth 1 -not -name "." -mtime +$AGE'
I am maintaining my script on github, so feel free to grab it. Again, THIS WILL DELETE DATA AND YOU HAVE BEEN WARNED.