Getting disk space usage from the command line is simple. The
df command shows disk usage per filesystem. The
du command shows disk usage for files, directories and subdirectories. This is great for simple things, however, doing an in-depth analysis is easier using some graphical tools.
This site has a very comprehensive list of tools available for use on your desktop. Professional tools are also available, but let's keep it simple.
Dealing with Multiple Filesystems and Directories
If you are using some of the basic tools, it is necessary to understand that all files live under "root", which is the "/" are the beginning of all full paths to files. This includes mounted file systems and special device files. It is tempting to use this command to get a summary of the top level directories on Linux:
$ sudo -i # du -sh /*
This will attempt to get disk usage on all mounted filesystems and directories that do not have regular files, like "/dev". Running
du on a mounted USB drive can be maddeningly slow. So, what to do?
Disk filesystems are composed of blocks of data "mounted" on a directory. This provides a "handle" to the device where it's filesystem is kept. The
mount command tells you which block devices are, but it includes a bunch of "junk" you don't really want to see.
df provides a nicer list because it only shows directories that actually contain files.
If you really just want filesystems, you can take advantage of the fact that filesystems are always block devices under "/dev".
mount | grep ^/dev will do the trick.
Speeding Up Creating Summaries
Now that you know how to use
du -s you'll notice it is still very slow. Let's say you find a big file after waiting 60+ seconds for
du -s to complete, remove it, then run
du -s again. Wait another 60+ seconds, and repeat for while. This takes a long time. You'll need to use more sophisticated tools. Let's stay with using the command line. This article has a pretty good explaination of how to deal with performance:
I looked at ncdu. Although this is a nice program, it is very basic. If you wnat to try it, it's a simple install:
$ sudo apt install ncdu
I'm used to using
DaisyDisk for the Mac and
Windirstat on Windows.
duc is a almost as efficient as these tools, but it can also be used on the command line with "curses" graphics. Which is very cool. It also keeps a database of the scan it performs. This works out very well. It's a simple install, but not quite obvious:
$ sudo apt install duc-nox
This installs the
duc-nox command, which is "curses" based. I've not looked for the
duc command yet. I'm going to try using
duc-nox for a while to see how well it performs.
I like to use the command line a lot.
duc can be installed on Mac OS. This script has a basic procedure, which doesn't quite work for me yet.