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


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: 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 | # ]