Deleting files from user home with script

jfriedel
New Contributor II

Hi, 

I have just run into this issue recently whereby I try to remove vulnerable apps that are not installed to /Applications folder but copied and being run from Desktop or Downloads folders.

Tried this easy command in terminal locally and worked like charm:

rm -R ~/Downloads/Visual_Studio_Code.pkg

So then I created a policy that would use a script with the same exact command to remove the file and ended up having this:
Script result: rm: /var/root/Downloads/Visual_Studio_Code.app: No such file or directory

Cannot I just use tilde for specifying the currently logged in user's Download folder? Why does it say /var/root?
I have 100+ users and not fancy writing a separate script for each of them where I just use the full path.
I'm sure I'm overlooking some obvious scripting rules here so all advice would be greatly appreciated!



 

1 ACCEPTED SOLUTION

_gsm
New Contributor III

Using ~ specifies the working directory and causes issues when Jamf runs the script as root. I tend to use full paths.

The following variable will specify the current user.

currentUser=$(/bin/ls -l /dev/console | /usr/bin/awk '{print $3}')

 

Making your command look like this.

rm -R /Users/$currentUser/Downloads/Visual_Studio_Code.pkg

View solution in original post

2 REPLIES 2

_gsm
New Contributor III

Using ~ specifies the working directory and causes issues when Jamf runs the script as root. I tend to use full paths.

The following variable will specify the current user.

currentUser=$(/bin/ls -l /dev/console | /usr/bin/awk '{print $3}')

 

Making your command look like this.

rm -R /Users/$currentUser/Downloads/Visual_Studio_Code.pkg

jfriedel
New Contributor II

Hi!

Thanks for the tip! It worked perfectly fine.
Appreciate it!