Removing files for all local users

macmanmk
Contributor

I'm trying to figure out the best way to accomplish removing two files from every local account on a Mac. The files are in the user Library at /Users/username/Library/Preferences and /Users/username/Library/Preferences/ByHost

The file at the top level of the user Preferences folder is named identically for all machines. However, the file inside ByHost is appended with the hardware UUID, which is going to be different on each machine. Can I use a wildcard in the script to remove that file or are wildcards generally discouraged? I'd like the script to run whether a user is logged in or not. Willing to try anything if there is a better option.

4 REPLIES 4

CSCC-JS
Contributor II

Here's the Google Chrome removal script I'm using.
You can use the wild card * in the path to selective delete specific folders / files across all the user accounts.

#!/bin/zsh

# Kill Google Chrome if running
killall "Google Chrome"

# Delete Google Chrome.App
rm -rf "/Applications/Google Chrome.app"

# Delete Google User Profile Information
rm -Rf /Users/*/Applications/Chrome Apps*
rm -Rf /Users/*/Library/Application Support/Google/Chrome*
rm -Rf /Users/*/Library/Google/GoogleSoftwareUpdate*
rm -Rf /Users/*/Library/Google/Google Chrome Brand.plist

exit 0

erichughes
Contributor II

Using the wildcard in the path doesn't seem to work for me. even with a sudo it only removes items from the user logged in. If it run sudo rm -Rf /Users/*/Library/Application Support/Folder123 knowing that exists in my logged in user and others, it only removes mine, it still exists when I check the other user. Catalina OS. Basically trying to script removal of a Application and its scattered bits. Pointers appreciated.

PaulHazelden
Valued Contributor

You would need to send the script from JAMF to have it run on the computer as Root. That way the script has the ability to access all of the User folders.
I would normally extract the usernames from the /Users folder and set them in a loop to work on all of the folders.

# Find the accounts on the Mac
HOMES=$(ls -l /Users | /usr/bin/awk '{print $9}' | /usr/bin/grep -viE '(root|jamf|shared|admin|.DS_Store|.localized)')
# ls -l /Users Lists all possible accounts
# /usr/bin/awk '{print $9}' strips it down to just the folder name
# /usr/bin/grep -viE '(root|jamf|shared|admin|.DS_Store|.localized)' Removes folders that match this pattern from the list
# $HOMES should be a list now
# And the for loop will split that down to be each line of the list, and keep going until the list is finished

# loop through the accounts
for USER_HOME in $HOMES
do
# Remove the File or folder you want to get rid of in existing User accounts
rm -Rf /Users/$USER_HOME/Library/Application Support/Folder123
# Add any other rm commands here
done

erichughes
Contributor II

Thank you very much. That will be very handy.