Posted on 11-17-2020 11:26 PM
Hi,
I'm looking for a script to run for all users so that it deletes files older than 60 days from the user's trash (preferably from "date added" to the trash, not when file was created), but either way is fine.
I can run some commands as some suggested from local terminal, but I can't executed from Jamf Pro:
find ~/.trash -mindepth 1 -mtime +60 -delete
Please advise.
Thanks in advance.
Solved! Go to Solution.
Posted on 11-18-2020 10:31 PM
The problem is your use of ~
to the path to the home directory. ~/.Trash
evaluates to the home of the user running the command, and when that script gets run from Jamf, it evaluates to the root account, meaning ~/.Trash
, becomes /private/var/root/.Trash
and not the logged in user's Trash as you're expecting it to.
You have to get the current user's username, and use that as part of the full path for it to work.
Try this instead:
#!/bin/sh
current_user=$(scutil <<< "show State:/Users/ConsoleUser" | awk '/Name :/ && ! /loginwindow/ {print $3}')
find /Users/${current_user}/.Trash -mindepth 1 -mtime +60 -delete
Posted on 11-18-2020 06:58 AM
What isn't working right with the find
command you posted when run from Jamf Pro?
Posted on 11-18-2020 07:43 PM
Hi wmehilos. Thank you for responding to my post. I'm not sure what's not working, but I can tell you when I test the script via Self Service, nothing is deleted.
But if I run the command to list the files via terminal:
find ~/.trash -mindepth 1 -mtime +60 -ls
I can see the files that command will delete.
Posted on 11-18-2020 10:31 PM
The problem is your use of ~
to the path to the home directory. ~/.Trash
evaluates to the home of the user running the command, and when that script gets run from Jamf, it evaluates to the root account, meaning ~/.Trash
, becomes /private/var/root/.Trash
and not the logged in user's Trash as you're expecting it to.
You have to get the current user's username, and use that as part of the full path for it to work.
Try this instead:
#!/bin/sh
current_user=$(scutil <<< "show State:/Users/ConsoleUser" | awk '/Name :/ && ! /loginwindow/ {print $3}')
find /Users/${current_user}/.Trash -mindepth 1 -mtime +60 -delete
Posted on 11-19-2020 03:10 AM
mm2270 is correct, but I fear also his solution will not help on Macs running 10.15, as the .Trash appears to be out of reach for the shell:
find: /Users/thisuser/.Trash: Operation not permitted
Automation on macOS becomes more and more difficult. Soon the macOS will be so secure that it is useless :(
Posted on 11-19-2020 08:48 AM
@mschroder Is that error coming from when the command is run out of a Jamf policy? Because I'm not seeing that issue myself. Can you elaborate on what happened?
Posted on 11-19-2020 09:38 AM
If have several devices on which the shell has no permission to access .Trash. I have not tried via jamf, but only locally, with and without sudo. On some nodes it was fine, on others it failed. Strange thing is that 'ls -l@d ~/.Trash' shows no extended attribute, which is what I expected to find.
Posted on 11-30-2022 02:46 AM
I know this is an old post but I came across the same issue. Make sure that the app running the script has full disk access (in my case CodeRunner)
Posted on 11-21-2020 02:54 PM
Thank you, mm2270! The script worked.
Thank you, everybody, for your time.