Joe Maller.com

Faster Trash Size AppleScript

For whatever reason my Finder sometimes takes forever to calculate the size of the Trash. That doesn’t include the clicks of opening a Trash window, selecting all, then getting info. Here’s a script which is much faster: Faster Trash Size (click to open in Script Editor)

Nothing special, just a quick AppleScript wrapper for this simple terminal command: du -h ~/.Trash | tail -rn1 | awk '{print "Size of Trash is " $1}'

It’s three quick commands:

  • du -h ~/.Trash
    Disk Usage, the “-h” flag reports sizes in human-readable values, the directory to report is ~/.Trash, which is the current user’s Trash folder. This dumps a size report for every item in ~/.Trash followed by a total at the bottom. That output is then piped out to tail:
  • tail -rn1
    Tail reads the end of files or the standard input, in this case the output of du. The “-r” flag reverses the output, then “-n1” prints one line. The output of this has only one line which looks like 2.9G /Users/joe/.Trash. That gets sent to awk for some simple formatting:
  • awk '{print "Size of Trash is " $1}'
    Awk is an amazing tool that I’ve only recently scratched the surface of. This particular command is a really just a simple echo statement which uses Awk’s column splitting ability to quickly split the output of du and tail. “$1” refers to the first column of the output, split on white space by default. I added a descriptive string to Awk’s print command to make the output a little more meaningful and prettier.

3 Responses to “Faster Trash Size AppleScript” Comments Feed for Faster Trash Size AppleScript

  • You can simplify this a bit by adding the -s flag to du which means that you no longer need the tail command. Eg.

    $ du -sh ~/.Trash | awk ‘{print “Size of Trash is ” $1}’
    Size of Trash is 8.0K

    Hope that’s helpful :-)

  • Thank you so much. This was just what I need to help me with a script I am trying to write.

  • Thanks very much for writing and posting this script. It is a great solution to a truly annoying quirk in the Mac OS.

Leave a Reply