Submit Hint Search The Forums LinksStatsPollsHeadlinesRSS
14,000 hints and counting!

Fast trash emptying via Unix script turned into an app Apps
I was tired waiting for the Finder to prepare the Trash emptying and then watching it delete the stuff one by one. So I wrote the shell script below, used Platypus to make an app, and have it on my desktop for single-click, fast trash emptying.
#/bin/sh
cd ~/.Trash
dirs=`find ~/.Trash -type d | wc -l`
fils=`find ~/.Trash -type f | wc -l`
byts=`du | tail -1`
(( dirs = dirs -1 ))
echo "$dirs: directories"
echo "$fils: files "  
echo "$byts: bytes "   

rm -Rf ~/.Trash/*
echo Return code: $?
To try, you can open an empty file in a text editor, copy the code, and save the file (i.e cleantrash.sh) on your Desktop. Then control-click and choose Open With from the conextual menu, and select Terminal from the list of apps.

[robg adds: I tested this one, and it worked as described. For more on Platypus, see this older hint. (The URL for the app has changed, though, so use the link above in this hint. Finally, this previous hint covered emptying the trash via script in a somewhat different manner. Check your trash before testing this script, just in case there's something there you really want -- it'll be gone for good once you run the script.]
    •    
  • Currently 2.13 / 5
  • 1
  • 2
  • 3
  • 4
  • 5
  (8 votes cast)
 
[11,640 views]  

Fast trash emptying via Unix script turned into an app | 15 comments | Create New Account
Click here to return to the 'Fast trash emptying via Unix script turned into an app' hint
The following comments are owned by whoever posted them. This site is not responsible for what they say.
Fast trash emptying via Unix script turned into an app
Authored by: tedw on Jul 21, '09 07:55:08AM
Platypus is fine if that's what you want to do, but there's no need for it in this case; this script can be run straight from OS X using applescript. just add the above script into a text string and use applescript's do shell script command to launch it, as follows:
set theScript to "#/bin/sh
cd ~/.Trash
dirs=`find ~/.Trash -type d | wc -l`
fils=`find ~/.Trash -type f | wc -l`
byts=`du | tail -1`
(( dirs = dirs -1 ))
echo \"$dirs: directories\"
echo \"$fils: files \"  
echo \"$byts: bytes \"   

rm -Rf ~/.Trash/*
echo Return code: $?"

do shell script theScript
paste this is Script Editor, and either save it as an application to run on its own, or run it as a script from QuickSilver, Butler, apple's Script Menu, or whatever other script launcher you might use.

[ Reply to This | # ]
Fast trash emptying via Unix script turned into an app
Authored by: wallybear on Jul 21, '09 10:39:30AM
There's an error in the script: du returns block count (with blocks of 512 bytes), not bytes.
Use the parameter -k if you want Kb, or, better, use the -h parameter for "human readable" value (with Kb/Mb/Gb scaling).
Moreover, using the -s parameter you can omit the 'tail -1' command; so:

byts=`du | tail -1`

becomes

byts=`du -sh`


[ Reply to This | # ]
Fast trash emptying via Unix script turned into an app
Authored by: wallybear on Jul 22, '09 11:05:41AM

mmmhmm... my comment was for the main article, not an answer to the above reply.



[ Reply to This | # ]
Fast trash emptying via Unix script turned into an app
Authored by: encro on Jul 21, '09 02:11:22PM

That is really a waste of what Applescript is meant for.

All you really need to do is rename cleanup.sh to cleanup.command and set the executable bit for a double clickable file.

mv cleanup.sh cleanup.command
chmod 755 cleanup.command


---
Steve



[ Reply to This | # ]
Fast trash emptying via Unix script turned into an app
Authored by: frgough on Jul 21, '09 10:17:19AM

I'm not so sure this is wise. Correct me if I'm wrong, but I think OS X does more than just delete files when emptying the trash. I think it's also doing housekeeping to make sure pointers, references and other geeky OS stuff relating to the file also gets cleaned up.



[ Reply to This | # ]
Fast trash emptying via Unix script turned into an app
Authored by: ClarkGoble on Jul 28, '09 08:57:50AM

I'm fairly sure this isn't the case. The only place I could see a problem is if you do this and a program is using a file. But that's a rare condition.



[ Reply to This | # ]
Fast trash emptying via Unix script turned into an app
Authored by: slb on Jul 21, '09 04:29:14PM

Simply save as or change to ".command" from ".sh"
It will run upon double-clicking.



[ Reply to This | # ]
Fast trash emptying via Unix script turned into an app
Authored by: hypert on Jul 22, '09 09:08:37AM

The .command extension has to be the longest, dumbest, non-standard extension I've ever seen. Is there any way to modify Mac OS (or Terminal?) to recognize executable .sh scripts (or others, like .pl scripts!) as double-clickable apps?



[ Reply to This | # ]
Fast trash emptying via Unix script turned into an app
Authored by: tedw on Jul 22, '09 08:24:48PM
that would be a bad idea, even if it is possible. do you want every one of the hundreds upon hundreds of scripts tucked away in your system to suddenly become double-clickable? if you do this kind of thing frequently enough that typing the word 'command' becomes an annoyance, then use and applescript like this that converts the file through drag&drop.
property scriptFileExtensions : {"sh", "pl"} -- insert others as needed

on open theScriptFiles
	tell application "Finder"
		repeat with thisScript in the theScriptFiles
			if name extension of thisScript is in scriptFileExtensions or name extension of thisScript = "" then
				set cmd to "chmod +x " & quoted form of POSIX path of thisScript
				do shell script cmd
				set name extension of thisScript to "command"
			end if
		end repeat
	end tell
end open


[ Reply to This | # ]
Fast trash emptying via Unix script turned into an app
Authored by: S Barman on Jul 23, '09 07:11:30AM

Of course the last echo line will always say that the return code is zero as long as you use the -f flag on the rm command!



[ Reply to This | # ]
Fast trash emptying via Unix script turned into an app
Authored by: Anonymous on Jul 23, '09 11:19:23AM
Yes, this annoys me too. So on the basis of the above comments, plus a bit of my own input, save the following into a file named "Empty Trash.command".
#/bin/sh
TRASH=~/.Trash
dirs=$(( $( find $TRASH -type d | wc -l ) - 1 ))
files=$( find $TRASH -type f | wc -l )
size=$( du -hs $TRASH | awk '{print $1}' )
echo "$size in $dirs directories and $files files"
rm -r $TRASH/*
ERROR=$?
if [ "$ERROR" != "0" ]; then
	echo "Failed with return code $ERROR."
fi

Get Info on the file, and make it executable. The extension makes it unnecessary to use Applescript or any other external tool. Why complicate the issue?

  • First thing I did: we're operating on the same directory several times over. Put it in an environment variable. It makes it more readable.
  • Next, "dirs=...": don't operate on a variable, then do something else, then return to that variable. Do it all in one step.
  • Use awk to pull only the size from du into size.
  • Reword the report to be more Mac-like.
  • Ditch the "force" option from rm because we'd prefer rm to fail if there's a non-existent file, which would point to a filesystem error.
  • Only return an error when there is one.

That's it.

[ Reply to This | # ]

Fast trash emptying via Unix script turned into an app
Authored by: memco on Jul 25, '09 12:03:22PM

This is equivalent to a secure empty trash right? Is there a need to run the delete free space command in diskutil after this? I remember seeing an article that said doing so after a normal Finder "Empty trash" was much faster than a secure empty)?



[ Reply to This | # ]
Secure delete
Authored by: scooby509 on Jul 26, '09 01:31:28PM

No, this is not the same as secure delete. ("Secure delete" means adding an overwrite step before deletion.)

Add the -P option to the rm command to force rm to overwrite files before deletion. But once you're doing that, you gain no speed advantage over having the Finder do it.



[ Reply to This | # ]
What empty trash does vs. rm
Authored by: scooby509 on Jul 26, '09 01:28:20PM
I haven't seen it documented, but there's not a whole lot the Finder needs to do.

The rm command calls unlink which is the normal way of deleting files in Unix. Source of rm is here, do a search for unlink(f) to see where unlink is called.

The unlink library call passes this request down to whatever file system you're using, and that's where all the complex stuff of managing various inodes and btrees and whatnot happens. So using Unix commands to delete stuff should not corrupt your file system. (Though it is possible, the -d option to rm is one way.)

Now, there is stuff like file associations and other stuff the Finder could take care of any time an app is installed or deleted. In cases where this is vital, developers usually release installer packages to make sure it happens. But most of the time, apps have everything they absolutely need stashed away in their app bundle. Generally, with that kind of stuff it's nice to have it tidied up, but not vital. And obviously the Finder doesn't attempt to clean up preferences or anything like that that litters your home directory.

One thing that did occur to me was that maybe the Finder tries to prevent you from deleting part of a package. So I did a little test: (Adjust that number if you have a slower or faster computer, and beware that this creates a ton of little files.)

mkdir test.app
cd test.app
for (( i = 0; i < 10000; i = i + 1 )); do echo "foo" > $i; done

Now, I know that rm -r test.app will, if interrupted, leave test.app with half its contents deleted. As it turns out, the Finder (as of 10.5.7) will do the same thing.

So if the Finder is slower, my guess is that it is being more careful about scanning for potential problems (rm -r will just throw up an error as it goes) and displaying a pretty progress bar.

My verdict: unless you're deleting some apps or preference panes or stuff like that, just use the script. Even if you do delete an app, there's no reason it should hose your system, and the Finder doesn't give much more of a guarantee anyway.

[
Reply to This | # ]

Fast trash emptying via Unix script turned into an app
Authored by: Glub on Oct 15, '09 09:31:16AM

What about emptying .Trashes on all the other disks beside boot?
Should we just "cd" into every .Trash and .Trashes then empty them one at a time?

Thanks for the hint



[ Reply to This | # ]