As you may already be aware, I also write guides for Alligator.io. As of
late, I’ve been covering a lot of command-line tools and tricks as part of a
“Command-line Basics” series.
I’m also a bit of a scatter brain sometimes, and last month ended up starting on
this particular blog post, realizing about half way through that I had already
written a post on analyzing disk usage.
I had stashed the post in my home directory as I didn’t necessarily want to
throw away something I put some effort into. Today as I struggled to come up
with a topic for my blog this week, I happened upon the file and figured it’s
day has come!
Without further ado…
The moment your system runs out of persistent storage (a/k/a disk space) things
can go off the rails quite quickly. Applications begin to behave erratically and
you can forget about saving whatever you’re working on. Things are even worse on
a remote server since there’s no GUI to growl at you about it. As per usual, the
command-line has you covered when you need to check what your current disk usage
is looking like.
For this post we’re going to be using the commands
du. Both are
pretty standard issue on Unix-like operating systems such as Linux and macOS.
To find out if you have them available, simply try running them.
On the off chance you don’t have these commands available, please consult with
your system’s package manager to see about remedying the situation. If you
happen to have a remote server available, you can always SSH in and follow
along at home.
None of the commands in this post are destructive in nature.
Checking total and available disk space
The quickest and easiest way to check your the total size, usage and
availability of all of your mounted disks is to run
df without any arguments.
Filesystem 1K-blocks Used Available Use% Mounted on
dev 16398928 0 16398928 0% /dev
run 16407640 1688 16405952 1% /run
/dev/nvme0n1p2 982940416 234606476 698333592 26% /
tmpfs 16407640 791960 15615680 5% /dev/shm
tmpfs 16407640 0 16407640 0% /sys/fs/cgroup
/dev/nvme0n1p1 523248 108564 414684 21% /boot
tmpfs 16407640 7468 16400172 1% /tmp
tmpfs 3281528 68 3281460 1% /run/user/1000
Without any arguments,
df shows the size in 1K blocks (1024) not to be
confused with 1kB blocks (1000).
Those large numbers can be intimidating, but at the very least, we can easily
grok what percentage of the disk has been used, and the current mount point.
To corral those blocks a bit, we can pass in the
argument to display the sizes in the power of 1024.
$ df -h
Filesystem Size Used Avail Use% Mounted on
dev 16G 0 16G 0% /dev
run 16G 1.7M 16G 1% /run
/dev/nvme0n1p2 938G 224G 666G 26% /
tmpfs 16G 786M 15G 5% /dev/shm
tmpfs 16G 0 16G 0% /sys/fs/cgroup
/dev/nvme0n1p1 511M 107M 405M 21% /boot
tmpfs 16G 7.3M 16G 1% /tmp
tmpfs 3.2G 68K 3.2G 1% /run/user/1000
Much easier to understand at a glance!
I take things a step further by aliasing
df -h so I never have to
worry about passing in the argument:
alias df='df -h'
Checking disk usage of a particular directory
Finding the total disk…
And that’s when the light bulb went off and I started to feel like I had written
this post already. The section that was left unfinished was going to cover using
du, which you can read about over on the original post.