Tracking Down Disk Usage on the Command Line

When I bought my Macbook a few months ago, one of the hardware choices I made was to get a 128GB solid state drive with it. While I love the performance of my SSD, its small size has given me some problems when trying to manage my disk usage.

Disk Overload

A few days ago, I opened the activity monitor and was shocked to see that my machine was reporting less than 4 Gigabytes of free space left on my disk! The worst part was that I had absolutely no idea what was taking up all of that space. Was it all the downloads I had saved from Chrome? My music library?

I began hunting around in finder and on the command line with du, but I could only account for a few gigs that way. I needed a way to recursively search my directories and find files and folders that were taking up unnecessary space. A cursory Google search didn’t find any apps or bash commands that would do exactly what I wanted, so I began to experiment.

Checking Disk Usage with bash

I knew that I wanted my output to be something like the tree command, so that’s where I started. A quick look over the manual entry for tree revealed the -h option, which prints out a human-readable size for each file output, such as [26M] or [1.3G]. This was a good start, but I also wanted the directories themselves to have a size printed. Fortunately tree also has a --du option, which does just that.

Tree with these two options is essentially what I wanted, the only caveat being that unless I ran it on a very small directory, the amount of output was far to much to sift through. Fortunately since the file size printout was in a nice consistent format, a simple ack filter did the job of only showing files above a certain size (100 Megabytes in my case).

The final command that I came up with was:

tree -ah --du . | ack '[(d{3,}M|d+.*G)]'

I aliased this command to the name bigfiles, and by running it on a few of the directories in my home directory, I was able to quickly track down a lot of unnecessary files and folders eating up my disk space. Within 20 minutes I was able to go from under 4 Gigbytes of free space to over 30.
 

Conversation
  • Topher says:

    You should post this on commandlinefu.com , that’s pretty awesome.

  • Tony O'Halloran says:

    Slightly off-topic, but there’s also an awesome graphical tool that does a similar job called Disk Inventory X that I’m a fan of, it’s worth a look!
    [http://www.derlien.com/]

    Back on-topic, I can’t believe I was ignorant of ‘tree’ and its bells and whistles all my life!

  • Comments are closed.