How can you check disk usage on a Linux server (using the `du` and `df` commands)?

Linux 3 min read

If you want to find out how much storage space is still available on a Linux server, or which files or folders are taking up the most space, the df and du commands provide a quick answer. df shows the total size of the file systems, whilst du shows the size of individual directories and files.

The df command: total disk usage

df (disk free) displays the total, used and free space on mounted file systems:

df -h

The -h (human-readable) switch displays values in a readable format (K, M, G) instead of raw block numbers. The df -hT command also displays the file system type (e.g. xfs, ext4), whilst df -h /var only shows the file system on which the specified directory is located. The tmpfs and devtmpfs entries in the list are temporary file systems in memory; they do not occupy disk space.

Inodes

A file system can run out of space even if df -h indicates there is still free space: every file occupies an inode, and the number of inodes is finite. With many small files (such as cache or session files), the inodes will run out first. To check:

df -i

Our separate article explains what an inode is.

The ‘du’ command: folder and file sizes

du (disk usage) shows the actual amount of disk space occupied by a folder or file:

du -sh /var/www

The -s (summary) switch only displays the total size, whilst the -h switch provides a human-readable format here too.

Finding the largest folders

The following command lists the direct subfolders of the specified directory in descending order by size:

sudo du -h --max-depth=1 /var | sort -rh | head -n 15

Once you have entered the largest subdirectory, you can repeat the command until you find what is taking up the space. When scanning the entire root directory, add the -x switch (du -xh --max-depth=1 /) to prevent du from traversing into other file systems.

The largest individual files (in this case, those larger than 500 MB) can be finded using the find command:

sudo find / -xdev -type f -size +500M -exec ls -lh {} \; 2>/dev/null

If you would like an interactive, menu-based view, install the ncdu program (apt install ncdu, or on AlmaLinux from the EPEL repository: dnf install ncdu). Further options for the du command (exclusions, size thresholds, hard links, sparse files, inodes) are explained in detail in the article on the du command.

Common placeholders

  • Log files: the files in /var/log and the error_log files for websites. There is a separate article on locating and removing large error_log files. The systemd log can be checked using the journalctl --disk-usage command, and its size can be reduced, for example, using the journalctl --vacuum-size=500M command.
  • Old backups and archives that have remained on the server.
  • Package cache: can be cleared using the dnf clean all or apt clean command.

If df and du show different values

If df reports that the disk is full, but du cannot find that much data, it is likely that a running program is still keeping a deleted file open. The solution to this is described in the article ‘The disk is full, but du doesn’t show it’.

Why is this important?

A full disk can cause serious problems: services may go down, and database operations and backups may fail. If you regularly run out of storage space and cleaning up the system does not help, you can choose a KVM VPS plan with more storage. On cPanel hosting, disk usage can also be monitored via a graphical interface; see: Checking disk usage in cPanel.

Back: Linux articles (62)