Skip to main content
Solved

Deleting files from user home with script


Forum|alt.badge.img+3

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!



 

Best answer by _gsm

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

2 replies

Forum|alt.badge.img+8
  • Contributor
  • 33 replies
  • Answer
  • January 24, 2023

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


Forum|alt.badge.img+3
  • Author
  • New Contributor
  • 7 replies
  • January 27, 2023
_gsm wrote:

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


Hi!

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


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