I had fun using the du command in Emacs this morning (that’s what I do when I don’t sleep well; judge away).

In Linux (and macOS), the DU command (I believe it stands for Disk Usage) is usually used to figure out what folders take up space on your hard drive.

While different GUI tools exist, they are not useful on encrypted drives on Linux, as they show the encrypted blocks instead of the directories, which don’t really tell you much (you could probably run those with escalated permissions, but I didn’t try).

For a gamer like me, it’s useful to see which games take up the most space. Here’s an example with a few useful flags, in the command line:

du -hs ~/.steam/steam/steamapps/common/* | sort -h

Shows me the following:

    4.0K    /home/jtr/.steam/steam/steamapps/common/Steam.dll
    76K     /home/jtr/.steam/steam/steamapps/common/Steam Controller Configs
    100K    /home/jtr/.steam/steam/steamapps/common/SteamLinuxRuntime
    248M    /home/jtr/.steam/steam/steamapps/common/Steamworks Shared
    657M    /home/jtr/.steam/steam/steamapps/common/SteamLinuxRuntime_soldier
    776M    /home/jtr/.steam/steam/steamapps/common/SteamLinuxRuntime_sniper
    1.4G    /home/jtr/.steam/steam/steamapps/common/Proton - Experimental
    3.4G    /home/jtr/.steam/steam/steamapps/common/Deep Rock Survivor
    3.5G    /home/jtr/.steam/steam/steamapps/common/Deep Rock Galactic
    11G     /home/jtr/.steam/steam/steamapps/common/Hades II
    21G     /home/jtr/.steam/steam/steamapps/common/Yakuza Kiwami
    82G     /home/jtr/.steam/steam/steamapps/common/Space Marine 2
    132G    /home/jtr/.steam/steam/steamapps/common/Helldivers 2

Some of the Steam-related components are not games, but… Helldivers 2 takes how much space?? Anyway.

A quick review of the options I used:

-hs for human readable (shows space in G for gigabyte and M for megabyte instead of writing in kilobytes) and summary (shows the top directory only)

~/.steam/steam/steamapps/common/* for the target path (this is where Steam stores the games in Linux, at least in Debian distros)

| to pipe the du command it into another command to read it better, in this case:

sort -h the sort command, which will sort it nicely again by order in human format. We need this last part if we want to see the directory in order, with the biggest one at the bottom.

Some places recommend using sort -hr, the additional r for reverse, which means in this case we will see the biggest directory at the top of the list. I don’t need it, because I want to see the biggest folder at the bottom, near the command line, which is where I’m going to focus next.

In Emacs, this command is easier use and works better thanks to Dired. Find a folder, mark it (m) in dired, and run dired-do-shell-command (!) and follow up with du -hs.

But since we’re in Emacs, and we might want to work with the results as text, we could use dired-do-async-shell-command (&). This will place the output in a temporary buffer we can work with (so we can save it to a text file, for example, with C-x C-w).

And here’s another thing I didn’t know: you can run these commands in Dired on multiple directories. Just mark several directories in Dired, and the resulting buffer will give you a list of all the directories you’ve marked. If you have this saved as a text buffer, it’s pretty easy to work withthe results (for example, save it as an org file and add headers telling you what you want to do with each directory).

By the way, even though it’s somewhat redundant with Dired’s default listing of files, you can also add the a option for du in this case ( for all) to display the files in the directories you’re viewing. This is useful in cases like the above, where you’re already working with the du command in Emacs and interested in looking at individual files as well, not just directories. Of course, you can just go in and list the files in Dired and open another Dired buffer with another directory listing files by size… this is Emacs, you have many ways to do whatever you want.