5

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.

bmike
  • 235,889
Marcel
  • 153
  • 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 Aug 29 '18 at 23:40
  • A folder can have its own trashbin? – Marcel Aug 29 '18 at 23:44
  • 1
    The End goal was just being able to clear all the trash programmatically. – Marcel Aug 29 '18 at 23:44
  • 2
    Questions about software development are off-topic here, but can be asked on Stack Overflow. – user3439894 Aug 29 '18 at 23:49
  • Oh sorry, I just assumed it's a rather apple specific question. As it's just about how the trash folders work. – Marcel Aug 29 '18 at 23:50
  • 1
    No. This is on topic here. Scripting is explicitly allowed in [help] @user3439894 – bmike Aug 30 '18 at 01:43
  • 1
    @bmike, It is my understanding that Go is programming language that is statically typed, and a compiled language in the tradition of C, not a scripting language. So I believe when someone states "I am writing a little piece of software in Go" then that makes it software development and is or should be off topic. – user3439894 Aug 30 '18 at 02:10
  • 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 Aug 30 '18 at 02:33

1 Answers1

4

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' 
bmike
  • 235,889