How to use du

Thanks to df, we know how to find information about the overall health of our disks, but we’re left to our own devices to find the details, specifically as to what’s taking up the most space.  The good news is that we’re in luck, as I’ve learned a trick from a great hunter that will help us through this task:  Just look for ducks!

That’s right … ducks.

Since I don’t care to sound completely out of sorts, I suppose an explanation is in order.  Much like df (Disk Free), there is another utility that reports on disk usage, and it is simply du.  If you were to execute du in a given directory, it will spit out file size information for every file system object (files, directories, links, et cetera) in the current directory, recursively:

~/sparse_directory $ du
4 ./directory1
4 ./directory5
4 ./directory7
4 ./directory2
4 ./directory6
4 ./directory4
4 ./directory3
4 ./directory9
4 ./directory8
44 .

While that’s handy, I think we could probably use a more condensed, less nonsensical view of the information.  We’re in luck again, as there are several options that can be passed to du that will actually give us exactly what we want.  Here they are, complete with their descriptions from the du manual:

  • -c
    This produces a grand total at the end of the report.
  • -k
    This causes file sizes to be reported in kilobytes rather than bytes.
  • -s
    This displays only the total for a given filesystem object rather than the individual file sizes of all of the files therein.

So, my notion is that using the command du -cks * will produce something that’s a little more meaningful than watching a ton of information fly by at the speed of a raging TTY.  Let’s see:

~/sparse_directory $ du -cks *
4       blah.txt
4       directory1
4       directory2
4       directory3
4       directory4
4       directory5
4       directory6
4       directory7
4       directory8
4       directory9
40      total

Okay, so, that’s admittedly not really all that much better, but the thing is that the sparse directory that I’m using for an example (in the fine tradition of useless examples) is not terribly realistic.  That being the case, let’s take a look at my collection of entertaining media from my external hard drive:

~/entertainment $ du -cks *
25676772        audio
93775456        video
119452228       total

Twenty-five gigabytes of audio, and around ninety gigabytes of video sounds about right.  If I were in a bind for space, all things considered, I would start deleting things from the video directory, most likely, as it is the larger of the two and thusly probably has the most benefit to gain in pruning for the lest amount of destruction.

There’s one more trick that we can pull if we really want the data ordered, and it involves something that we learned about when we looked at basic plumbing.  We can pipe the output of our ducks command through the sort utility to get a list that is sorted by ascending disk usage:

~/entertainment $ du -cks * | sort -n
25676772        audio
93775456        video
119452228       total

I’ve got another bad example there, as the audio directory is alphabetically before the video directory in the first place, so the order is unchanged from the previous example.  At this time, I think that we’ve become good enough chums that you can take my word that the above really does sort the output.  What’s great is that since the line containing the total is always going to have the largest number associated with it, it will always show up at the end of that list.

Of course, if you were to walk out into a field and scream “DUCKS,” all of the ducks would likely move quickly to the furthest possible point from your location.  Instead, it might be a good idea to use a duck call, as it were.  Drop this into your bash profile and re-source it, and you can have this handy duck call, quack, available to you all the time:

alias quack="du -cks * | sort -n"

That’s all that I can really say about ducks right now.  Until next time, please be very, very quiet.

===

Liquid Web’s Heroic Support is always available to assist customers with this or any other issue. If you need our assistance please contact us:
Toll Free 1.800.580.4985
International 517.322.0434
support@liquidweb.com
https://manage.liquidweb.com/

Be Sociable, Share!
Here's $75, Launch a New VPS Today. Find out why 30,000 customers have chosen our Best-in-Class Performance & 24x7 Heroic Support.