Script to look for specific home folder

andrew_betts
New Contributor II

Hey everyone.  Looking to create a smart group, via an Extension attribute, that looks for every MacOS device that has the home folder 'test'.  So /Users/test.  Having trouble putting together the script for the extension attribute that can look or every device with this home folder.  Would appreciate some help.

I then want to delete that home folder for all confirmed devices by scoping the smart group and using the command 'sudo rm -rf /Users/test'.  Does this seem like a reasonable way to accomplish this?

I guess I could just blast every device with this command, but that seems a little unwise...  😉

 

EDIT- I got the script figured out (see below), but still looking for advice on best approach for deleting the home folder on devices that have it.  Thanks.

 

#!/bin/bash

if [ -d /users/ ]; then
echo "<result>Has test Folder</result>"
else
echo "<result>Does Not Have test Folder</result>"
fi

exit

 

1 ACCEPTED SOLUTION

andrew_nicholas
Valued Contributor

This should probably accomplish what you need for detection. 

#!/bin/sh

path="/Users/test"
result="false"
if [ -d "$path" ]; then
    result="true"
fi

echo "<result>$result</result>"

View solution in original post

4 REPLIES 4

andrew_nicholas
Valued Contributor

This should probably accomplish what you need for detection. 

#!/bin/sh

path="/Users/test"
result="false"
if [ -d "$path" ]; then
    result="true"
fi

echo "<result>$result</result>"

andrew_betts
New Contributor II

Thanks.  Works great.  Thought I had it with the script I just posted, but my version seems to be giving inaccurate results.

sdagley
Esteemed Contributor II

@andrew_betts The script you posted is just checking for the root directory /users (which should be /Users as in @andrew_nicholas's example) not a folder for a user named "test" inside it. If that folder belongs to an actual user you _really_ do not want to just delete the folder. I forget what version of macOS added the command, but the preferred  way to delete a user since at least macOS 10.12 is the sysadminctl command, and to delete the user test, and their folder in /Users, you'd run this command:

 

/usr/sbin/sysadminctl -deleteUser test

 

Thanks.  No, the folder doesn't belong to any local user.  The situation is that sometime ago, I composed a pkg via Composer and didn't exclude Users or Library when packaging- so all devices that installed the app got some random home folder (named after the test user I was composing with).  I don't think there is any harm in the folder being there and the contents seem to be of no consequence, but still- thought it would be best to delete.  I could have worked backwards and just deleted the home folder by scoping  the devices that took the pkg, but it has gone through a few iterations, only one of which gave devices the extra home folder.  Hope that makes sense.