Per-directory Disk Usage from CLI
There are many tools out there for checking disk usage of directories. However, command-line users have a very powerful tool called… well, shell, what else. :) The Linux shell has many small tools that aren’t necessarily very powerful on their own, but do a beautiful job when used together. Here we will combine a few tools to get the list of largest subdirectories in the current directory.
find
The find command is one of the most useful command-line tools when you want to manipulate lists of files. We will use find to get the list of subdirectories under the current directory.
find -type d
This will list all directories. Of course, this will recursively list all subdirectories as well, and we don’t want that. So we will limit the recursion depth to one level:
find -maxdepth 1 -type d
Nice, we now have the list of current subdirectories. Note that -maxdepth switch has to come before anything else.
du
du (short for disk usage) is a handly little tool that will tell us how much some file or directory takes up on our hard drive. If you just execute du, it will show you the sizes of all files in all directories and subdirectories below the current directory.
We don’t want that. How about this:
du -s
It shows you the summary size of the contents of the current directory. If you want a subdirectory, you have to pass it as an argument.
du -s mydir/
The number isn’t very nice, though. So let’s fix that first:
du -sh mydir/
Now we get a so-called ‘human-readable’ number, but we are still limited to one subdirectory. And I’m getting a feeling that you’ve already guessed where this is going. Yes: back to the find command.
du + find
Without further ado:
find -maxdepth 1 -type d -execdir du -sh {} +
Now that’s more like it! We have a list of subdirectories with their sizes.
du + find + sort
Another handy little tool is the sort command. For our purposes, we need to call with with -h and -r switches. This gives us sorting by human-readable values (which we used with du) and reverse sorting, respectively.
find -maxdepth 1 -type d -execdir du -sh {} + | sort -rh
As with most *NIX tools, sort accepts input from stdout. This means we can pipe the output of the previous command into the sort call using the | (pipe operator).
Adding an alias
If you will be using this command often, you should add an alias to the above:
alias dircap='find -maxdepth 1 -type d -execdir du -sh {} + | sort -rh'
Now you can just say dircap instead of that longish command.
Making it a bit more convenient
Sometimes you are only interested in the top ones. So:
dircap | head
If you want to see them all, using a pager like less would make it a bit more convenient:
dircap | less
osd_cat
If you use a window manager which supports binding shortcuts to random commands, you might try this one:
dircap | head -n 4 | osd_cat
This will temporarily display a small overlay with the first 4 lines (the -n 4 switch) of the dircat command. On Arch Linux, osd_cat is part of xosd package.




