6

So, I am writing a little piece of software in Go. It is a library that allows clearing the trashbin and moving files into the trashbin. However besides ~/.Trash there are other trash folders per drive. What would be the most correct way of clearing those?

Is it just iterating over /Volumes and deleting the .Trashes on every volume and recreating it afterwards?

Is there official documentation on it, if so I couldn't find it.

8
  • 1
    Won’t the system just make these again - the design is to keep a per unit trash where units are user home folders and volumes. What is the end goal here?
    – bmike
    Commented Aug 29, 2018 at 23:40
  • 1
    The End goal was just being able to clear all the trash programmatically.
    – Marcel
    Commented Aug 29, 2018 at 23:44
  • 2
    Questions about software development are off-topic here, but can be asked on Stack Overflow. Commented Aug 29, 2018 at 23:49
  • 1
    No. This is on topic here. Scripting is explicitly allowed in help center @user3439894
    – bmike
    Commented Aug 30, 2018 at 1:43
  • 3
    I know @user3439894 but it's not so much about go as where the trash files are. You're 100% right it could be asked on SO and you'll surely get a different reception and answer there, so I see your comment as really trying to help. I just keyed on the "off topic" - go scripting is fine here, as is swift scripting, bash scripting, tcl scripting, AppleScript (you get the pattern I think...)
    – bmike
    Commented Aug 30, 2018 at 2:33

1 Answer 1

5

Spotlight can locate folders with a specific name on all mounted and indexed volumes (but it's not going to be the answer - bear with me):

mdls ~/.Trash/

This will show you that the kMDItemFSName is ".Trash" so you might try mdfind to do a spotlight search

mdfind "kMDItemFSName == '.Trash'"

Sadly, this won't find the trashes since Apple has made them invisible and probably excluded them entirely from the spotlight indexing. But, this is the best Apple way to search for general files.

So now, you need to crawl the filesystem:

find / -name .Trash -print

This will throw filesystem errors, so you'll need to engage root - be careful with sudo - you can ruin a system so you can't boot if you move or delete files (which is what you're about to do - find things and then delete them)

 sudo find / -name .Trash -print

Even without the sudo you'll find most of the trashes since your user should be able to write to Trash to store files there.

Now, there is a tool faster than find (it may take tens of minutes or perhaps hours if it starts crawling remote or connected drives)

locate .Trash

The locate database won't likely be built when you ask for it, so follow the instructions it prints if you don't have a pre-made locate database to consult. Happy scripting. Feel free to ask a follow on question on how to delete if you need that help too, but this answers how to find files specifically hidden system ones like the various trash files.

Or, you know - just tell Finder to empty the trash from the command line:

Boom, now you're done super fast no matter where or how the folders are named.

osascript -e 'tell app "Finder" to empty' 
0

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .