Delete shortcut from all users desktops

Sobchak
Contributor

I ran a script that included: sudo rm ~/desktop/"skoolbo" It worked on 4 our of 5 of my test machines so I assumed that the command was correct, but I keep getting: Script result: rm: /var/root/desktop/skoolbo: No such file or directory

Why did the command work on some computers but not others, and what command should I have ran instead?

2 ACCEPTED SOLUTIONS

dsavageED
Contributor III

As a script, something like:

#!/bin/bash
# create an array
_Users=( `ls /Users | grep -v "Shared"`)

echo ${_Users[@]}

# remove shortcut
for TheUser in ${_Users[@]}
do
rm -f /Users/$TheUser/TheShortcut
done

exit 0;

View solution in original post

ryan_ball
Valued Contributor

Here is how you can remove a list of files from all user's desktops on the Mac.

#!/bin/bash

userList=$(/usr/bin/dscl . list /Users | grep -v "^_" | grep -v nobody | grep -v daemon | grep -v root)
fileNames=(
    file1
    file2
    file3
    file4
)

for userName in $userList; do
    userHome=$(/usr/bin/dscl . read "/Users/$userName" NFSHomeDirectory | awk '{print $2}')
    for fileName in "${fileNames[@]}"; do
        file="$userHome/Desktop/$fileName"
        if [[ -e "$file" ]]; then
            echo "$file exists; deleting."
            rm -Rf "$file"
        else
            echo "$file does not exist; skipping."
        fi
    done
done

exit 0

View solution in original post

12 REPLIES 12

cody_anderson
New Contributor

Root is true super user account on the machine but it also has to be enabled so chances are the Root user was not configured to have that folder on the desktop or the Root user is not enabled.
To enable Root to check you would need to open and unlock the Directory Utility (can be found through Sys Prefs -> Users and Groups -> Login Items or Spotlight Search it).
Once the panel is unlocked you can find "Enable Root User" under the "Edit" option in the task bar and set a password.
Once its enabled reboot the computer and log in using the Username Root and the Password of your choosing.

It should be noted again that Root is a super user account and will not ask you to confirm passwords before making changes to the OS or System libraries so you can accidentally break a ton of stuff if you are not careful.

Sobchak
Contributor

So what command should I have used to removed the shortcut from all users desktops? Or I guess any user that has the shortcut?

dsavageED
Contributor III

As a script, something like:

#!/bin/bash
# create an array
_Users=( `ls /Users | grep -v "Shared"`)

echo ${_Users[@]}

# remove shortcut
for TheUser in ${_Users[@]}
do
rm -f /Users/$TheUser/TheShortcut
done

exit 0;

Sobchak
Contributor

What about using:

#!/bin/sh
sudo rm -r /applications/"skoolbo.app"
sudo for /f %%a in ('dir /B /AD C:Users') do ( if exist "C:Users%%aDesktopskoolbo" del /F /Q "C:Users%%aDesktopskoolbo"
)

The goal is to remove the app and any desktop shortcuts that have been created.

I guess in my rush to find the answer I ended up grabbing a Windows script.

chris_miller
Contributor

You should be able add that to the script above and do both.

chris_miller
Contributor

I used the following in that script for the shortcut path for an alias for Calculator I created.

rm -f /Users/$TheUser/Desktop/Calculator

chris_miller
Contributor

So you might use something like this to do the app and the shortcut:

rm -f /Users/$TheUser/Desktop/MacKeeper
rm -f /Applications/MacKeeper.app

ryan_ball
Valued Contributor

Here is how you can remove a list of files from all user's desktops on the Mac.

#!/bin/bash

userList=$(/usr/bin/dscl . list /Users | grep -v "^_" | grep -v nobody | grep -v daemon | grep -v root)
fileNames=(
    file1
    file2
    file3
    file4
)

for userName in $userList; do
    userHome=$(/usr/bin/dscl . read "/Users/$userName" NFSHomeDirectory | awk '{print $2}')
    for fileName in "${fileNames[@]}"; do
        file="$userHome/Desktop/$fileName"
        if [[ -e "$file" ]]; then
            echo "$file exists; deleting."
            rm -Rf "$file"
        else
            echo "$file does not exist; skipping."
        fi
    done
done

exit 0

Sobchak
Contributor

I ended up using:

#!/bin/bash
sudo rm -r /applications/"skoolbo.app"
# create an array
_Users=( `ls /Users`)

#echo ${_Users[@]}

# remove shortcut
for TheUser in ${_Users[@]}
do
    if [ -f "/Users/$TheUser/Desktop/skoolbo" ]; then
        sudo rm -f /Users/$TheUser/Desktop/skoolbo
        echo sudo rm -f /Users/$TheUser/Desktop/skoolbo
    fi
done

exit 0;

This seems to do what I need. Looking at some of your scripts maybe more echos would be useful though.

Sobchak
Contributor

How do you get you script to look right in posts?

ryan_ball
Valued Contributor

@Sobchak Use the little >_ button above where you create your comment.

Sobchak
Contributor

Thanks that looks much better. I wish hovering over all those button told you what they did.