Delete all files from desktop

UlrikS
New Contributor III

Hi all,

My goal is to delete any files or folders on the desktop when the user logs out.

I'm using this command in bash script: rm -Rf /Users/<currentuser>/Desktop/*

The sript is set to run at logout.

Unfortunately it does not work.

When I try the command in the users Terminal it prompts "sure you want to delete all X files in /Users/<currentuser>/Desktop [yn]?"

According to this post under Jamf Pro the script should work if the script is set to run at 'startup', but this option is not available in Jamf School.

Is there any way to get it to work?

21 REPLIES 21

obi-k
Valued Contributor II

Does the Guest Account meet your needs?

UlrikS
New Contributor III

No. Would have been nice if it did but unfortunatly it doesn't in this case.

rob1
Contributor
It works when I type in rm -Rf /Users/accountname/Desktop/* and set it to run at logout. 
There are a few things to consider however: I had to give the bash application permission for Full Disk
Access under Settings>Privacy & Security (I have Ventura OS installed) and I had to create an exception
for /bin/bash app in my Ransomware Shield on my Antivirus program.
Also just checking that you did type in the actual account name of the user and not currentuser.

 

UlrikS
New Contributor III

I had given full disk access to the Terminal (I guess that is the native bash application) and set the shell option rmstarsilent (to suppress the y/n prompt - though I guess this is not necessary when the script is run through Jamf), but it just doesn't seem to work. I am not running an antivirus program or anything else that I would suspect could prevent access to the files.

I have tried running the script at login, at logout, on a schedule, and as at one time script. Every time Jamf reports the script was run with no exit code but nothing happens.

If I just type “rm -Rf /Users/accountname/Desktop/*” into the Terminal, it works (after setopt rmstartsilent - otherwise I have to type y to delete the files/folders).

And yes, I did type the actual account name 😊

Try reinstalling your Scripting module that is installed under organisation>settings, this shoud re-install the Jamf School Scripting daemon and the generated certificate on the Macs. Perhaps there is an issue there?

 

UlrikS
New Contributor III

Ok, I removed the scripting module and installed it again, but the result is still the same: Jamf logs another run of the script, and the time of execution with the exit code "No error" - but the files on the desktop aren't touched.

I reinstalled the iMac I'm using to test, just to see if this would change anything, but it didn't.

Hmm strange, just as a test, try altering your script somewhere along these lines and set it to run at login:

#!/bin/sh

rm -Rf /Users/accountname/Desktop/*

 Failing that, then I think maybe log a support ticket.

I just tested on one of my newer Macs and came across the same issue, it reports as if it has run but doesn't actually delete the files on the desktop. Think maybe it is an issue with login and logout hooks on newer Mac OS, you may need to set something up like this instead:

https://forums.macrumors.com/threads/deleting-user-files-signing-out-of-programs-on-every-logout.237...

UlrikS
New Contributor III

This is getting a bit strange... I can't get it to work like that either. "sudo defaults write com.apple.loginwindow /Users/Shared/scriptToRun.sh" seems to executes without any errors but the script isn't being run. Or it is being run and someting is preventing it from deleting the files.

I tested that the script works as a login item, which it does, but this is not really what I want.

I'll try fidling more with it, and eventually start a ticket with Jamf.

UlrikS
New Contributor III

I tried with #! /bin/sh, /bin/bash and /bin/zsh, but nothing changes.

I agree that it might be time to submit a ticket. 

sgiesbrecht
Contributor III

Depending on which version of macOS you are using, Logout hook no longer works
The Login hook still does, so you can do it at that time, or run the command daily on Recurring Check-in

UlrikS
New Contributor III

I am on Monterey 12.6.3.

I tried running the script on both login, logout, on a schedule and as a ‘just once’ script, and whatever I choose I see the execution counter going up, and the ‘executed at’ time change.

It seems the Script is being run, but something is preventing it from working.

try this 

 

rm -rvf /Users/<User>/Desktop/*.*

 

-r = recursive
-v = verbose ( will show you what is deleted )
-f = force

UlrikS
New Contributor III

I changed the script to include *.* and -v, but I still get the same result. The script runs according to Jamf, but the files remain on the desktop.

If I just run the scriptline in Terminal on the machine it works.

What is / was the output of the script

UlrikS
New Contributor III

When it runs through Jamf, there is no output.

If I make it into a .command file and runs it I get this:

Last login: Mon Mar 13 15:57:24 on ttys000
/Users/user/Documents/ClearUSerFiles.command ; exit;                            
user@Test-iMac ~ % /Users/user/Documents/ClearUSerFiles.command ; exit;

Saving session...
...copying shared history...
...saving history...truncating history files...
...completed.

[Process completed]

Not really usefull and no files are deleted.

If I open a terminal and run the script from inside the terminal there is no output at all, but the script works. If I specify /Users/user/Desktop/*.* only files are deleted, but /Users/user/Desktop/* will delete everything.

UlrikS
New Contributor III

Following the guide in the link the script runs fine when executed and both files and folders are deleted.

How about running the executable file on the mac as a startup item using a profile?

I see from a previous post that this isn’t what you want though. Time to log a ticket with Support, your reseller may be able to either help you or will escalate your issue to someone who does.

UlrikS
New Contributor III

It works if I make it as an Applescript:

 

#!/usr/bin/osascript
tell application "Finder"
  delete every item of folder "Apple HD:Users:user:Desktop"
  empty the trash
end tell

 

But only as "Just once" or on a schedule.

This is however not a solution.