Trash directories on mac - How to find them and how to properly clear them
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.
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:
- How can I empty the trash from the Terminal?
Boom, now you're done super fast no matter where or how the folders are named.
osascript -e 'tell app "Finder" to empty'