Move folder to trash via Self Service

robb1068
Contributor

Trying to set up a Self Service policy to delete a folder (with additional folders and files in it), located in the user's home folder. Current script I'm using is below:

!/bin/bash
loggedInUser=$(ls -l /dev/console | awk '{ print $3 }')
"rm -rf ~/Documents/folder/folder/folder/"

I've tried different variations (rmdir, rm -r, without " marks, leaving off the final /), but no luck. Works fine on deleting a file though and running the rm -rf command via ARD as the current user also works. All things being equal, I'd really like to just move the directory to the user's trash, but again the script works fine with a file, but not a folder.

Any help or suggestions would be appreciated, thanks!

7 REPLIES 7

drtaru
New Contributor III

You're getting the current user correctly but then not supplying it to the rm command and instead specifying ~

Edit the RM line to the following:
"rm -rf /Users/$loggedInUser/Documents/folder/folder/folder/"

samuellarsson
New Contributor III

Just a quick note to make the distinction: the rm command doesn't move the file/folder to trash, it deletes it permanently. To actually move the file/folder to the trash of the logged in user, the simplest way is most likely to use the mv command to move the file/folder to /Users/$loggedInUser/.Trash/.

robb1068
Contributor

Yes, I would rather move the folder to the user's trash, because they're currently manually trashing files and I'd like to replicate what the usually see. And I have a script that does that without issue (mv ~/Library/Preferences/filename ~/.Trash), but that's trashing individual files and not a directory. But mv (and mvdir) were not working on trashing a folder, so I moved on to rm -rf to just let the policy delete the directory.

Unfortunately I'm not seeing any errors in the log. The policy reports that it succeeded, but the folder isn't deleted.

AdamCraig
Contributor III

I did this with a script once. What I did was

#!/bin/bash
CURRENTUSER=$3

mv /Applications/AppName.app /Users/$CURRENTUSER/.Trash

What I found was that it was failing on about 30 of the 1000+ computers it ran on because they had an older version of that app in their trash.
The mv command won't over write it if there is another file with the same name.

samuellarsson
New Contributor III
But mv (and mvdir) were not working on trashing a folder

mv does work for all folders and files, as long as you have the permissions to move it, so there is probably something else that is making the command not work (folder path name issue perhaps, like the one dealt with by @drtaru above).

The mv command won't over write it if there is another file with the same name.

It will if you use the force flag -f.

robb1068
Contributor

Thanks for the responses everyone! I was able to build out both an rm and mv scripts and spin out policies for self services.

I think there was indeed something off about my original rm -rf script, but I couldn't see what the problem was for the life of me. The path was pretty simple, but it failed every time. I built the mv script and then cloned it, loping off the /.Trash at the end.

The one caveat I ran into with using the mv -f script is that it works fine with sending files to the trash and overwriting the existing file, but folders would only move once. If the folder was already in the trash the mv -f would fail.

eWhizz
New Contributor II

You should probably mv the files to a dated folder in the trash, just in case. Not good practice to overwrite files if you can help it.