Skip to main content
Solved

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

  • November 18, 2020
  • 8 replies
  • 16 views

Forum|alt.badge.img+3

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.

Best answer by mm2270

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 original
Did this topic help you find an answer to your question?

8 replies

wmehilos
Forum|alt.badge.img+11
  • Valued Contributor
  • 69 replies
  • November 18, 2020

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


Forum|alt.badge.img+3
  • Author
  • New Contributor
  • 7 replies
  • November 19, 2020

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
Forum|alt.badge.img+24
  • Legendary Contributor
  • 7885 replies
  • Answer
  • November 19, 2020

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

Forum|alt.badge.img+12
  • Valued Contributor
  • 359 replies
  • November 19, 2020

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
Forum|alt.badge.img+24
  • Legendary Contributor
  • 7885 replies
  • November 19, 2020

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


Forum|alt.badge.img+12
  • Valued Contributor
  • 359 replies
  • November 19, 2020

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.


Forum|alt.badge.img+3
  • Author
  • New Contributor
  • 7 replies
  • November 21, 2020

Thank you, mm2270! The script worked.

Thank you, everybody, for your time.


Forum|alt.badge.img+10
  • Contributor
  • 166 replies
  • November 30, 2022
mschroder wrote:

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)


Reply


Cookie policy

We use cookies to enhance and personalize your experience. If you accept you agree to our full cookie policy. Learn more about our cookies.

 
Cookie settings