One of the most important pieces of information on a server is how full the disk is. Servers commonly become unresponsive or freeze when the main disk is full, and it can be difficult to recover from that situation. In this article, we’ll show you how to check the disk usage on your Linux machine using the df utility to see how full the disk is and the du utility to understand what’s using all that space.
We’ll assume you have a machine running any Linux distribution. The df utility is a standard utility from the 1970s and so is available on every Linux distribution as well as on Macs in the terminal.
Step 1 – Get System-Level Information
We’ll start with just the basic version of the command. To check how full your disk(s) are, just run
And you’ll see a list of disks along with how much space on them has been used. The -h flag just tells df to output in human-readable units, otherwise the numbers will be in bytes and take the entire screen. If you want different units, use
-Band the first letter of the unit you’d like (e.g. -BM for megabytes, -BT for terabytes)
Each line has information on the filesystem it represents, the size of that disk, the amount of space that’s in use on that disk, the amount used as a percentage, and the mount point for that filesystem. This disk-by-disk overview is the most helpful for understanding if you have an issue with disk usage.
One additional tip is to include the
--totalflag to include a line that summarizes every disk on the system. This is useful for getting a system-wide overview, but remember that most programs will crash if disk they are writing to is full even if there is another disk with space available.
Step 2 – Get Directory-Level Information
While df offers a disk-level view of the file system and disk usage, many times you’re also concerned with what files are using all that space. That’s where du comes in. A companion to df, du tells you the size of individual files and directories on disk. To use it, just pass a file or directory name. For example, to see the size of all files in your home directory, run
du -h ~
And you’ll see one line of output for every file and directory in your home directory. Note that the output is typically very long, much longer than df. This is because du will recursively walk through directories by default. To limit the output, use the “–max-depth” flag. For example, the following command will output the size of your home directory and all its children
du -h --max-depth=1 ~
Finally, note that the output from du isn’t sorted. Even with readable output provided by limiting the max depth, this can be frustrating. To make better use of the output, we’ll need to use a pipeline as in the next step.
Step 3 – Identify Outliers
We can combine the output from du into a bash pipeline to get the biggest “offenders” in terms of disk space. We’ll use the “sort” command to sort the output by file size, and we’ll use the “tail” command to limit the output to the worst 3.
du -h --max-depth=1 ~ | sort -h | tail -n 3
Because we use the -h flag in du for human-readable output, we use the same flag in sort (otherwise it will treat 6 kb as bigger than 5 MB). Sort outputs the lines with the largest file last, so we use tail to get the bottom 3 rows. To get more or fewer offenders, just change the number passed to “-n”.
Step 4 – Get More Info
While we used df just to get file-level info, it’s also useful for other filesystem info. For example, if you’re interested in inode information, you can run
And get info on the system inodes. Likewise, if you’re interested in which filesystem a given disk or partition is using, you can run
To get the full information for each unit, with the filesystem type under “type”. For example, the output here
Filesystem Type Size Used Avail Use% Mounted on rootfs lxfs 476G 181G 296G 38% / tmpfs tmpfs 476G 181G 296G 38% /dev tmpfs tmpfs 476G 181G 296G 38% /run tmpfs tmpfs 476G 181G 296G 38% /run/lock tmpfs tmpfs 476G 181G 296G 38% /run/shm tmpfs tmpfs 476G 181G 296G 38% /run/user cgroup tmpfs 476G 181G 296G 38% /sys/fs/cgroup /dev/sda4 ext4 476G 181G 296G 38% /mnt/c
In this article, we’ve seen how to use the programs df and du to gauge how full our disks are and to identify what’s using all that space. The output from df is typically very short and already in a useful order. To use du, it’s usually best to include some flags to trim the output down, and to include it in a pipeline to remove some redundant information. Both programs allow you to check disk usage and understand what’s using all your space on Linux and Mac systems.