25th Anniversary Savings | 25% Off Dedicated Servers*Shop Now
25th Anniversary Savings | 25% Off VPS Hosting* †††Shop Now
Limited Inventory: High-Performance AMD-Powered Servers Now Available.* Shop Now >
Dedicated Hosting Deals | From $99/moShop Now
Earn hosting credits and a chance to win an Amazon gift card when you refer friends to Liquid Web!Read our promo rules

How to Use the du Command in Linux

Posted on by J. Mays
Reading Time: 3 minutes

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, etcetera) 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 flag produces a grand total at the end of the report.
  • k – This flag causes file sizes to be reported in kilobytes rather than bytes.
  • s – This flag displays only the total for a given file system 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 thus 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 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

About the Author: J. Mays

As a previous contributor, JMays shares his insight with our Knowledge Base center. In our Knowledge Base, you'll be able to find how-to articles on Ubuntu, CentOS, Fedora and much more!

Latest Articles

How to Edit the PHP Memory for Your WordPress Site via WP Toolkit

Read Article

What is CGI-Bin and What Does it Do?

Read Article

Top 10 Password Security Standards

Read Article

Top 10 Password Security Standards

Read Article

How to Use the WP Toolkit to Secure and Update WordPress

Read Article