Skip to main content
Solved

Script to look for specific home folder


Forum|alt.badge.img+3

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

 

Best answer by andrew_nicholas

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 original
Did this topic help you find an answer to your question?

4 replies

Forum|alt.badge.img+13
  • Honored Contributor
  • 365 replies
  • Answer
  • July 16, 2021

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>"

Forum|alt.badge.img+3
  • Author
  • New Contributor
  • 6 replies
  • July 16, 2021

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


sdagley
Forum|alt.badge.img+25
  • Jamf Heroes
  • 3540 replies
  • July 16, 2021

@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

 


Forum|alt.badge.img+3
  • Author
  • New Contributor
  • 6 replies
  • July 16, 2021
sdagley wrote:

@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.


Reply


Cookie policy

We use cookies to enhance and personalize your experience. If you accept you agree to our full cookie policy. Learn more about our cookies.

 
Cookie settings