Need a script to delete files after X number days in the Trash

merladmin
New Contributor II

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.

1 ACCEPTED SOLUTION

mm2270
Legendary Contributor III

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

View solution in original post

8 REPLIES 8

wmehilos
Contributor

What isn't working right with the find command you posted when run from Jamf Pro?

merladmin
New Contributor II

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.

mm2270
Legendary Contributor III

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

mschroder
Valued Contributor

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 :(

mm2270
Legendary Contributor III

@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?

mschroder
Valued Contributor

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.

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)

merladmin
New Contributor II

Thank you, mm2270! The script worked.

Thank you, everybody, for your time.