Delete All Files in Desktop and Downloads

AHSitguy
New Contributor III

Hi,
I'm new to scripting and I'm trying to delete all files and folders within the Desktop folder and the Downloads folder without actually deleting the Desktop folder and Download folder themselves. I've tried using rm -rf /users/username/desktop/* , but it prompts for confirmation and then doesn't execute when trying to deploy through Jamf. Does anyone know how to do this? I want the trigger for the policy to be "at logout".

1 ACCEPTED SOLUTION

AHSitguy
New Contributor III

Using @PaulHazelden's terminal command with a trigger of "Startup" works. I couldn't get it to work at loginlogout though.

View solution in original post

7 REPLIES 7

PaulHazelden
Valued Contributor

Commands are case sensitive.

rm -Rf /Users/<username>/Desktop/*

Is what I would use. Deleting just the files should be available to the User, but root can do it too. How is your script being executed? Direct out of Jamf or as a local script. From Jamf it will be running as root. As a local script it could be running as the User.
I am guessing you have a way of finding the username, and adding it to the script?
For Log out I would add it as a script, and then add that to a policy, and set the policy to run at logout.

I am using this exact command, but it actually doesn't delete anything, as if Root doesn't have access to delete the folder. Specifically, my script is:

TargetUser=$3
rm -Rf /Users/$TargetUser/Desktop/*

But despite not throwing any errors, the script doesn't actually delete anything off of the user's desktop.

I use the Recurring Checkin trigger, and it doesn't delete anything. If I trigger it manually with a sudo jamf policy -id [ID] command, it actually works as expected. It's only when it's triggered by the recurring checkin that it fails.

Any thoughts on why it would fail to do anything during checkin, but work just fine when triggered manually?

AHSitguy
New Contributor III

I have found that the command doesn't like to run when the user profile is loaded. Did you try a different trigger, such as "startup"? I would suggest using a different trigger where the user profile isn't loaded. Reason being, if a user has a file open that was saved on the desktop, the command won't delete that file or it will fail entirely. If the user profile isn't loaded, then the user wouldn't have any files open and the command would be free to delete what is there.

None of these files are open, as the computers haven't been touched in a month and have been restarted several times.

The problem with using any other trigger is I don't want to have to specify the user in the script. Rather I would prefer it just target the "Currently logged in user."

What's more, as I said above, it works if I enter a sudo jamf policy command from the computer, locally, so clearly, the user being logged in isn't necessarily the issue.

AHSitguy
New Contributor III

@PaulHazelden I'm using the execute command function within Files and Processes in a Jamf Policy.

AHSitguy
New Contributor III

@PaulHazelden I tried your code and it doesn't come up with any errors in Jamf, but it also doesn't work on the Mac. When I run that code in terminal, it prompts for confirmation. How can I enter a Y or yes in the code so that it confirms the deletion?

AHSitguy
New Contributor III

Using @PaulHazelden's terminal command with a trigger of "Startup" works. I couldn't get it to work at loginlogout though.