Linux commands to see the sizes of your disks / SSD drives. Find how much used & free space you have. Find the largest directories.
We have multiple ways of checking what disk space is available on Linux systems, and how it's used. Let's go through the most usual methods.
Check Total Disk Sizes
To check the sizes of our disks, we can use the lsblk
command:
lsblk
In this output, we should look just at the entries that have disk
listed as their TYPE
. In this example, we can see we have a (virtual) disk, called vda
, which has a total size of 20G (20 gigabytes).
In the future, we'll probably see the letter T
here, for terabytes.
But, often, this won't be enough. The command just shows us how much storage space our disks / SSDs have available, in total. But what about how that space is used? How much our data is using, and how much is still free.
Whenever we write a file, or a directory, it's saved in a filesystem. Which means that only the filesystem will know how much space on our disks is actively used, and how much is free.
Check Disk Space on Filesystems
There is a Linux command, called df
, that will show us the following things:
- Total disk space on each filesystem.
- Used disk space on each filesystem.
- Free disk space available on each filesystem.
For example, a filesystem can have a total size of 100GB. But it might currently use just 40GB to store our data, so around 60GB will be free.
To see the total, free, and used disk space on our filesystems, we can use this command:
df -hx tmpfs
(Think of df
as disk free. We'll explain what -hx tmpfs
does, later on).
It's worth noting that this will only show us the filesystems that our Linux-based operating system is currently using. If some filesystems are not in use (not mounted), they won't show up in this command.
The output of the previous df
command looks like this:
Let's learn how to interpret it.
Understanding the Output of the "df" Command
df
organizes its output in a few columns. Here's what the columns on the right mean:
- "Size" shows us the total size of a particular filesystem. In this case,
9.8G
signals 9.8 gigabytes.K
is for kilobytes,M
is for megabytes, andT
is for terabytes. - "Used" shows how much of that total filesystem space is actually in use, how much data is stored on this filesystem.
- "Avail" is short for "available". And it shows us how much free space we still have (available) on this filesystem.
- "Use%" tells what percentage of the total filesystem space is in use. 42% means that 42% of the total 9.8 gigabytes available is currently in use. Which also means that 100-42=58% is currently free (not in use).
- "Mounted on" shows us where this filesystem is mounted. Where it is "attached" to our system. What directory we have to access to get "into" that filesystem.
It's important to note that filesystem sizes are shown here, which are not always the sizes of the entire disk/SSD. Since a filesystem can be created on just a small portion of a storage device. For example, a 2000GB SSD can be split into two partitions, each housing its own filesystem. So one filesystem could have a total size of 800GB, and the other a size of 1200GB.
On servers, one filesystem will often occupy the entire size of a disk / SSD. On home computers, a disk / SSD will often be split into multiple partitions, each with its own filesystem. It's not a rule set in stone, but it's what we'll usually encounter.
When we run the df
command, most of the times we'll want to look at the numbers displayed for the filesystem mounted in /
. Since that's the one storing the files, and directories of our operating system.
In some cases, we might also see a separate filesystem mounted in /home
. And that filesystem's purpose is to store the personal data of users: Documents, movies, music, program settings, and so on.
The /home
directory will exist on a normal Linux system, regardless. But sometimes it will be on its own filesystem, other times it will just be part of the main filesystem mounted in /
.
Excluding Temporary Filesystems from the "df" Command
So what does -hx tmpfs
do in the df -hx tmpfs
command we used? Well, if we run just df
, on its own, the output is much harder to read:
df
Note how we see 3971932
in one Used
column. But if we add the -h
option, which stands for "human readable", we get this:
df -h
Now we see 3.8G
instead of 3971932
. Which is much easier to read. So `-h` tells the `df` command to display sizes in human-friendly formats, like megabytes, gigabytes, and so on.
But also notice that we get a lot more filesystems in this output. Compared to our df -hx tmpfs
command that only showed us two filesystems.
Note the tmpfs
entries that are 5MB, or just a few hundred megabytes in size. They don't seem like anything related to our storage devices. Those are temporary filesystems. Since we aren't interested in them, we can tell our df
command to exclude "tmpfs" filesystems. And we do that by adding -x tmpfs
to our command. Along with our previous -h
option, we end up with:
df -h -x tmpfs
Which can be shortened to:
df -hx tmpfs
This gives us the shorter, cleaner output, focused on our real filesystems:
So that's how we arrived to the command included at the start of this blog.
But let's say we found a filesystem which is almost full. We see "98%" in the Use%
column. What do we do now? How do we fix this?
How to Find the Largest Directories on a Linux System
On a filesystem that's almost full, we'll want to know what is taking up so much space. What are the directories that store the biggest amount of data?
We can find the "largest directories" with this command:
sudo du -h --max-depth=5 / | sort -h
sudo
is required so that thedu
command, that follows it, can temporarily get administrative privileges. To make sure it can scan through every directory, even those that are protected (more specifically, the directories that our current user does not have permissions to read). If you don't have "sudo privileges" on a system, then just use the previous command without the firstsudo
word. Just rundu -h --max-depth=5 / | sort -h
.- Think of
du
as "disk usage" or "directory usage", to remember this command. -h
is for human readable numbers: Megabytes, Gigabytes, and so on. The same effect it had for thedf
command.--max-depth=5
tellsdu
to only go "five directories deep". You can adjust this value depending on how much you want to "zoom in" on your directory, and subdirectory sizes.- The
/
at the end tellsdu
to start scanning, starting with the top-level/
root directory. - Finally, we pipe to the output from the
du
command, to another command, by using the|
character. - We send the output to a
sort -h
command, which will sort these sizes from smallest to largest.-h
tellssort
to organize according to the "human readable" sizes, from kilobytes to megabytes, gigabytes, and so on.
We'll see output like this:
The largest directories, taking up most of our disk space, will be at the bottom.
In this screenshot, we can see that /
is the biggest directory, using 5.1GB of our disk space. But we can usually ignore this last entry. Because that's the root directory, that contains everything else. So it makes sense that it will always be the largest, since it "has everything inside". But anything above /
could be useful output.
If we go up one level, we can see that /usr
is taking up a lot of space, 2.4GB. And, more specifically, we can see that out of that 2.4GB, the subdirectory /usr/lib
is using up a large chunk of 1.7GB. And if we keep going up in this list, we can see that /usr/lib/firmware
is yet another subdirectory in /usr
that uses a large chunk of our storage space.
We go from a generic:
Hey, /usr
is using up a lot of space on my filesystem.
To:
Hey, it seems that in/usr
I have afirmware
directory that is taking up considerable space. Maybe I can delete some firmware that I don't need, for hardware I am not using.
Of course, in this case, a few hundreds megabytes is not a problem. But we can imagine scenarios where we might see directories that use up hundreds of gigabytes. And the previous command can help us dive in and pinpoint where this storage space is wasted.
Also, it's worth mentioning that we can move the starting point of du
from /
to something else. So after we scanned with a command like:
sudo du -h --max-depth=5 / | sort -h
We can move into that firmware
directory that was bothering us, to scan through it specifically. All we need to do is replace /
with the full path to the directory we want to "further zoom into", /usr/lib/firmware
in this case:
sudo du -h --max-depth=5 /usr/lib/firmware | sort -h
Now we took a deeper dive here, and we can see "mrv1" and "mellanox" are using up most of our storage space in this "firmware" directory.
Visually Inspect Directory Sizes with the "ncdu" utility
Another interesting utility to inspect directory sizes is the ncdu
utility. Which can be installed with:
sudo apt install ncdu
Or:
sudo dnf install ncdu
Depending on your distribution.
And then we can run ncdu
, followed by the directory we want to start scanning from. Which is usually the root directory /
:
ncdu /
We'll get output like this:
We can highlight the directory we want to inspect, with the UP/DOWN arrow keys, and then "zoom into" it by pressing the RIGHT arrow key.
We have /var
highlighted, so by pressing the RIGHT arrow key, we'll end up on this screen:
Now we're in /var
. With the LEFT arrow key we can go back, to the previous directory, /
:
So that's an alternative way we can inspect what directories are using up most of our storage space. It certainly makes it easier to navigate around the directories that interest us.
And just in case you need to remove an entire directory, see: How to Remove a Directory in Linux.
Conclusion
Hopefully this covers most questions about how to check disk space on Linux.
Anything we missed? Leave a comment if you think there's useful information we could add. Or if you think you could help our readers with a trick you learned along the way.