du command is one of the most popular Unix commands. It is used to quickly estimate and report disk usage by a certain directory tree, all figures are reported in n blocks of data consumed by each object. While most commonly used block size is 1024b these days, you can easily override this if you have to. It’s flexible enough to report sizes of only specified directories, full directory tree or even size of each file. There is no faster or easier way to find out what’s consuming most space on a certain filesystem – you usually run du for a high-level overview, and, once the largest directories are identified, use find command to highlight the largest files within them.
The simplest form of using du is when you run it with no additional parameters. In this mode, du will scan your current directory and all the subdirectories of it to calculate usage stats. By default, you get the usage reported by all the directories found under the specified (or default) location.
Important: be careful with default du behavior, because depending on your Unix OS, it may use different block size for reporting the size. Older systems would use 512-byte blocks, while most of the recent releases use 1024-byte ones. To avoid any confusion, get used to specifying the desired block size with -k parameter (see below).
Default du behaviour. I’m doing pwd before it just so that you know which directory it’s going to scan:
Like it was mentioned before, du calculates all the sizes in blocks of data. All the reports you see are not bytes or kilobytes, but the block sizes – so it’s good to always remember this. In most cases these days, the default block size is 1024 bytes, so you are effectively looking at all the sizes reported in kilobytes, but to make sure it’s always the case I advise you to use -k parameter which overrides you Unix flavour default block size and sets it to 1024 bytes:
If you wanted to have all your sizes in bytes, then there’s a -b parameter just for you:
Finally, most common releases of Linux have -h option for you, which reports all the figures in an even easier form, specifying K for kilobytes, M for megabytes and so on:
Using -a parameter, it is possible to have du report sizes of not only the directories, but all the files as well.
Compare this (directory sizes):
against this (files and directories):
The higher the level of disk usage you’re after, the larger lists will be produced by du. To cope with this, du has a -s option, which summarizes all the reports down to only the level of directories you specify. To be exact, it only reports size summaries of the directories you specify in command line. If you use this option but specify no directory, it will produce only one line of output, summarizing the size of the current directory:
And here’s how one would specify a few directories to summarize upon:
But like I said, the real power of this option can only be appreciated when dealing with many more directories. Take /usr for example, and compare…
… basic du approach, which shows size of each directory and subdirectory under /usr (I have stripped off the output):
against the summary du -sh output, which matches /usr/* mask and essentially shows the summary of only the first level of /usr subdirectories:
Do you see the difference? Summary like this allows me to instantly estimate which of the /usr subdirectories take up most space.