Remove Network (not mobile) Account Home Folders

cstout
Contributor III
Contributor III

I've found plenty of excellent scripts for removing mobile accounts that create a home directory on login but I haven't found any that remove network account home directories.

I have some lab computers that do not "create mobile account at login" and a user home folder is created. These users are not in dscl so the scripts like the example below do not work for me. I'm trying to clear out the home folders for these network logins but don't know enough about bash to rework this script.

#!/bin/sh

userList=`dscl . list /Users UniqueID | awk '$2 > 1000 {print $1}'`

echo "Deleting account and home directory for the following users..."

for a in $userList ; do

find /Users -type d -maxdepth 1 -mindepth 1 -not -name "*.*" -mtime +7 | grep "$a";

if [[ $? == 0 ]]; then

dscl . delete /Users/"$a"; #delete the account

rm -r /Users/"$a"; #delete the home directory

fi

done

Inputting this line in terminal outputs the correct list of user's folders that I want deleted:

find /Users -type d -maxdepth 1 -mindepth 1 -not -name "*.*" -mtime +7

I'm just not sure how to change the variables to remove the dscl lines and have it rm -rf these listed user folders.

1 ACCEPTED SOLUTION

mm2270
Legendary Contributor III

Ok if you're certain that the command above is listing out the exact accounts you want to remove, then just modify the script to use that as its source for the loop. Modified version below, not tested at all, so test, test and test some more!

#!/bin/sh

echo "Deleting home directory for the following users…"

userList=`find /Users -type d -maxdepth 1 -mindepth 1 -not -name "*.*" -mtime +7 | awk -F'/' '{print $NF}'`

for a in $userList ; do

rm -r /Users/"$a"; #delete the home directory

done

I would just be cautious its not also targeting legit home directories. When I run that find command on my Mac it list some accounts that are local, but not network based.

View solution in original post

7 REPLIES 7

mm2270
Legendary Contributor III

Two simple ways you can modify this to work.

Add an awk line to the end of your find command to only print out the user names like-

find /Users -type d -maxdepth 1 -mindepth 1 -not -name "*.*" -mtime +7 | awk -F'/' '{print $NF}'

This tells awk to use "/" as its field separator and print the last "field" which will be just the username(s)

Or, simply edit the "rm" line to something like this instead-

rm -r "$a"; #delete the home directory

Since your original find command you posted prints out items like /Users/someuser, just use that entire path as the directory to remove.

HTH

cstout
Contributor III
Contributor III

Hi Mike, I just did a quick edit and used the modified rm -r "$a"; line and it still leaves the folders behind. I think the reason for this is because the logic at the beginning of this script is pulling a list of users from dscl. These users aren't in dscl so that part returns an empty list. I'm wondering how to get rid of the dscl part and just have it list out the users from the find command and delete all those folders (there are a lot of them).

mm2270
Legendary Contributor III

So is there an easy way to distinguish these Network home accounts (by name or ID, etc) from regular local accounts that need to stay on there? If so, then I'd say yes it should be possible to produce a list of just the ones you want to nuke with find.
I don't use network only accounts, so I don't have anything to test with. Anything you can input on how they show up would help figuring it out.

cstout
Contributor III
Contributor III

@mm2270][/url: It's tricky because the accounts don't list in dscl since they aren't being created as mobile accounts. They don't list in system preferences either. The only item that gets left behind is a home folder that's generated on first login. I'm trying to make a housecleaning script for these lab machines that will pull this list:

find /Users -type d -maxdepth 1 -mindepth 1 -not -name "*.*" -mtime +7

and delete the results. That command lists exactly what I want removed, but I don't know how to write the logic to use the output of this list and delete it.

Edit:
I should specify that the output generated when I run that command is like this:

/Users/user1
/Users/user2
/Users/user3

mm2270
Legendary Contributor III

Ok if you're certain that the command above is listing out the exact accounts you want to remove, then just modify the script to use that as its source for the loop. Modified version below, not tested at all, so test, test and test some more!

#!/bin/sh

echo "Deleting home directory for the following users…"

userList=`find /Users -type d -maxdepth 1 -mindepth 1 -not -name "*.*" -mtime +7 | awk -F'/' '{print $NF}'`

for a in $userList ; do

rm -r /Users/"$a"; #delete the home directory

done

I would just be cautious its not also targeting legit home directories. When I run that find command on my Mac it list some accounts that are local, but not network based.

cstout
Contributor III
Contributor III

Thank you, I'll test it right away. The switches "-not -name" are successfully leaving behind /administrator and /Shared and only listing the network accounts. There are no other local accounts on these computers so the output does appear accurate to me. I suppose I'd have to go back to the drawing board again if I wanted multiple local accounts, but for now, that list is exactly what I want deleted. Thank you for your help!

cstout
Contributor III
Contributor III

@mm2270 Your modified version works flawlessly. Thank you!