- Classic SysAdmin: How to Check Disk Space on Linux from the Command Line
- df
- du
- Not as hard as you thought
- How to Check Disk Space in Linux
- Check Linux Disk Space Using df Command
- Display Usage in Megabytes and Gigabytes
- Understanding the Output Format
- Display a Specific File System
- Display File Systems by Type
- Display Size in 1000 Instead of 1024
- Check Disk Space in Linux With du Command
Classic SysAdmin: How to Check Disk Space on Linux from the Command Line
This is a classic article written by Jack Wallen from the Linux.com archives. For more great SysAdmin tips and techniques check out our free intro to Linux course.
Quick question: How much space do you have left on your drives? A little or a lot? Follow up question: Do you know how to find out? If you happen to use a GUI desktop (e.g., GNOME, KDE, Mate, Pantheon, etc.), the task is probably pretty simple. But what if you’re looking at a headless server, with no GUI? Do you need to install tools for the task? The answer is a resounding no. All the necessary bits are already in place to help you find out exactly how much space remains on your drives. In fact, you have two very easy-to-use options at the ready.
In this article, I’ll demonstrate these tools. I’ll be using Elementary OS, which also includes a GUI option, but we’re going to limit ourselves to the command line. The good news is these command-line tools are readily available for every Linux distribution. On my testing system, there are a number of attached drives (both internal and external). The commands used are agnostic to where a drive is plugged in; they only care that the drive is mounted and visible to the operating system.
With that said, let’s take a look at the tools.
df
The df command is the tool I first used to discover drive space on Linux, way back in the 1990s. It’s very simple in both usage and reporting. To this day, df is my go-to command for this task. This command has a few switches but, for basic reporting, you really only need one. That command is df -H. The -H switch is for human-readable format. The output of df -H will report how much space is used, available, percentage used, and the mount point of every disk attached to your system (Figure 1).
What if your list of drives is exceedingly long and you just want to view the space used on a single drive? With df, that is possible. Let’s take a look at how much space has been used up on our primary drive, located at /dev/sda1. To do that, issue the command:
The output will be limited to that one drive (Figure 2).
You can also limit the reported fields shown in the df output. Available fields are:
- source — the file system source
- size — total number of blocks
- used — spaced used on a drive
- avail — space available on a drive
- pcent — percent of used space, divided by total size
- target — mount point of a drive
Let’s display the output of all our drives, showing only the size, used, and avail (or availability) fields. The command for this would be:
The output of this command is quite easy to read (Figure 3).
The only caveat here is that we don’t know the source of the output, so we’d want to include source like so:
df -H --output=source,size,used,avail
Now the output makes more sense (Figure 4).
du
Our next command is du. As you might expect, that stands for disk usage. The du command is quite different to the df command, in that it reports on directories and not drives. Because of this, you’ll want to know the names of directories to be checked. Let’s say I have a directory containing virtual machine files on my machine. That directory is /media/jack/HALEY/VIRTUALBOX. If I want to find out how much space is used by that particular directory, I’d issue the command:
du -h /media/jack/HALEY/VIRTUALBOX
The output of the above command will display the size of every file in the directory (Figure 5).
So far, this command isn’t all that helpful. What if we want to know the total usage of a particular directory? Fortunately, du can handle that task. On the same directory, the command would be:
du -sh /media/jack/HALEY/VIRTUALBOX/
Now we know how much total space the files are using up in that directory (Figure 6).
You can also use this command to see how much space is being used on all child directories of a parent, like so:
The output of this command (Figure 7) is a good way to find out what subdirectories are hogging up space on a drive.
The du command is also a great tool to use in order to see a list of directories that are using the most disk space on your system. The way to do this is by piping the output of du to two other commands: sort and head. The command to find out the top 10 directories eating space on a drive would look something like this:
du -a /media/jack | sort -n -r | head -n 10
The output would list out those directories, from largest to least offender (Figure 8).
Not as hard as you thought
Finding out how much space is being used on your Linux-attached drives is quite simple. As long as your drives are mounted to the Linux system, both df and du will do an outstanding job of reporting the necessary information. With df you can quickly see an overview of how much space is used on a disk and with du you can discover how much space is being used by specific directories. These two tools in combination should be considered must-know for every Linux administrator.
And, in case you missed it, I recently showed how to determine your memory usage on Linux. Together, these tips will go a long way toward helping you successfully manage your Linux servers.
How to Check Disk Space in Linux
Managing disk space on a Linux server is an important task. For example, package manager applications notify you how much disk space will be required for an installation. For that information to be meaningful, you should know how much space your system has available.
In this tutorial, learn how to use the df command to check disk space in Linux and the du command to display file system disk space usage.
- A Linux-based system
- A terminal window / command line
- A user account with sudo or root privileges
Check Linux Disk Space Using df Command
You can check your disk space simply by opening a terminal window and entering the following:
The df command stands for disk free, and it shows you the amount of space taken up by different drives. By default, df displays values in 1-kilobyte blocks.
Display Usage in Megabytes and Gigabytes
You can display disk usage in a more human-readable format by adding the -h option:
This displays the size in kilobytes (K), megabytes (M), and gigabytes (G).
Understanding the Output Format
The df command lists several columns:
Filesystem Size Used Avail Use% Mounted on udev 210M 0 210M 0% /dev tmpfs 49M 1004K 48M 3% /run /dev/sda2 7.9G 4.3G 3.2G 58% /
Your output may have more entries. The columns should be self-explanatory:
- Filesystem – This is the name of each particular drive. This includes physical hard drives, logical (partitioned) drives, and virtual or temporary drives.
- Size– The size of the filesystem.
- Used – Amount of space used on each filesystem.
- Avail – The amount of unused (free) space on the filesystem.
- Use% – Shows the percent of the disk used.
- Mounted on – This is the directory where the file system is located. This is also sometimes called a mount point.
The list of filesystems includes your physical hard drive, as well as virtual hard drives:
- /dev/sda2 – This is your physical hard drive. It may be listed as /sda1, /sda0, or you may even have more than one. /dev stands for device.
- udev– This is a virtual directory for the /dev directory. This is part of the Linux operating system.
- tmpfs – You may have several of these. These are used by /run and other Linux processes as temporary filesystems for running the operating system. For example, the tmpfs /run/lock is used to create lockfiles. These are the files that prevent multiple users from changing the same file at the same time.
Display a Specific File System
The df command can be used to display a specific file system:
You can also use a backslash:
This displays the usage on your primary hard drive. Use the mount point (in the Mounted on column) to specify the drive you want to check.
Note: The df command only targets a full filesystem. Even if you specify an individual directory, df will read the space of the whole drive.
Display File Systems by Type
To list all file systems by type, use the command:
This lists drives with the ext4 type, in human-readable format.
Display Size in 1000 Instead of 1024
You can display disk usage in units of 1000 instead of 1024:
This can address a point of confusion in storage technology. Hard drive manufacturers sell hard drives in sizes based on 1000 bytes = 1 kilobyte.
However, operating systems divide that space up so that 1024 bytes = 1 kilobyte. Because of this, a 1000-gigabyte hard drive ends up with roughly 930 gigabytes of usable storage.
Note: Check out our overview of the Linux free command used to check total, used, shared, free, and available memory and swap space.
Check Disk Space in Linux With du Command
The du command displays disk usage. This tool can display disk usage for individual directories in Linux, giving you a finer-grained view of your disk usage. Use it to display the amount of space used by your current directory:
Like the df command, you can make du human-readable:
It displays a list of the contents of the current directory, and how much space they’re using. You can simplify the display with the -s option:
This shows how much space the current directory uses.
To specify the directory or file, check use the following options:
With the second command, you may have noticed a permission denied error message. This means the current user doesn’t have the privileges to access certain directories. Use the sudo command to elevate your privileges:
Note: If you’re working on CentOS Linux, you may need to use the su command to switch to the root user to access protected directories.
You should now understand how to use df and du commands to check disk space on your Linux system. Remember, to display a complete list of options, use either df —help or du —help .
Check out our article on how to use fsck command to run a filesystem check as preventive maintenance or when there is an issue with your system.