Linux Show Remaining Disk Space: df and du Explained

To show remaining disk space in Linux, use the df command (disk free) with the -h flag for human-readable output. Simply type df -h in a terminal to see a list of mounted filesystems, their total size, used space, available space, and mount points.

Why Disk Space Matters on Linux

Linux systems, especially servers and development machines, can fill up quickly with logs, package caches, container images, and user data. When a filesystem reaches full capacity, programs may crash, databases can corrupt, and the system may become unstable. Knowing how to check remaining space is a fundamental skill for anyone administering a Linux machine, whether it’s a personal laptop or a production server. The df and du commands are the standard tools for this task, and they are available on virtually every Linux distribution.

Using df to Check Free Space

The df command reports the amount of disk space used and available on all mounted filesystems. Here are the most common ways to use it:

  • df -h – Shows sizes in human-readable format (e.g., G, M, T).
  • df -T – Displays the filesystem type (e.g., ext4, xfs, tmpfs).
  • df -i – Shows inode usage, which is a separate limit on many filesystems.
  • df /home – Shows space for a specific directory or mount point.

For example, df -h / will show the remaining space on the root filesystem. The output includes columns for Filesystem, Size, Used, Avail, Use%, and Mounted on. The ‘Avail’ column is the remaining space you can use without root privileges.

Using du to See What’s Using Space

While df shows the big picture, du (disk usage) helps you find which directories or files are consuming space. Run du -h --max-depth=1 /path/to/dir to see the size of each subdirectory. To find the largest directories in your home folder, you might use du -h --max-depth=1 ~ | sort -h (sort by human-readable size). This is often the first step in cleaning up space.

How to Choose the Right Command

Use df when you need a quick answer to “how much space is left on this disk?”. Use du when you need to know why a disk is full or which files are taking up the most room. A practical workflow is:

  1. Run df -h to see which filesystem is full.
  2. Navigate to that mount point (e.g., cd / or cd /var).
  3. Run du -h --max-depth=1 | sort -h to list the largest subdirectories.
  4. Drill down into the largest directories until you find the culprit files.

Common Problems and Misconceptions

One common misconception is that df shows the same as the capacity of your hard drive. In reality, it shows the space available on each mounted filesystem, which may be partitions or logical volumes. Another issue is that deleted files may still hold space if a process has them open. If you delete a large file but df still shows it as used, check for processes holding the file with lsof +L1 (list open files with link count zero). Also, on some filesystems, inodes can run out before space does. Use df -i to check inode usage.

Alternatives to df and du

For a more visual or interactive view, you can use tools like ncdu (NCurses Disk Usage), which provides a text-based interface to browse directory sizes. There are also graphical applications like Disk Usage Analyzer (Baobab) on GNOME, or KDE’s Filelight. These are not installed by default on all systems, but they are available in most package repositories. For scripting, you can use df --output=avail to get just the available space number, which is handy for automated monitoring.

Safety and Best Practices

When cleaning up disk space, always double-check before deleting files. Use du to identify large files, but verify they are not needed (e.g., system logs, old kernels, or package caches). It’s safe to remove temporary files in /tmp or package manager caches, but avoid deleting files in system directories unless you know what they do. For important data, make backups before removing anything. Also, be cautious with rm -rf; always specify the full path to avoid accidents.

Frequently Asked Questions

What is the difference between df and du?

df shows the free space on a filesystem, while du shows the disk usage of files and directories. df is about the disk’s capacity, du is about what’s consuming it.

Why does df show more used space than the sum of all files?

This can happen because of reserved blocks for the root user, metadata, or deleted files still held open by processes. Also, du counts only files, not filesystem overhead.

How do I check disk space on a remote Linux server?

Use SSH to connect to the server and then run df -h in the terminal. You can also use tools like ssh user@host 'df -h' to run it directly.

Can I use df to check space on an unmounted partition?

No, df only shows mounted filesystems. To check an unmounted partition, you would need to mount it first or use tools like lsblk (list block devices) to see its size.

What does the ‘Use%’ column in df mean?

It shows the percentage of the filesystem that is in use. A value near one hundred percent indicates that the disk is almost full, which can cause performance issues or prevent writes.

Summary

Checking remaining disk space in Linux is straightforward with the df command. For a human-readable summary, use df -h. When you need to find out what’s consuming space, use du to drill down into directories. Remember to check inode usage with df -i if you suspect a different kind of full. These commands are essential for maintaining a healthy Linux system.