sodcalculator

10 Tech How-To

Check Disk Space in Linux: 6 Commands

Guide 10-37225 · Updated · Step by step

To check disk space in Linux, use the df command for file system usage and the du command for directory sizes. This guide covers those core tools plus ls -lh, fdisk, find, and ncdu, with examples and tips for interpreting the output.

Why Checking Disk Space Matters

Running out of disk space on a Linux system can cause application crashes, failed writes, and even database corruption. Monitoring disk usage helps you plan capacity, clean up logs, and avoid downtime. On a server, a full disk can prevent SSH logins because the system cannot write temporary files, so knowing how to check space quickly is a fundamental admin skill.

The df Command: File System Level

df (disk free) reports how much space is used and available on mounted file systems. Run it without arguments to see all mounted devices:

df -h

The -h flag shows sizes in human-readable units (KB, MB, GB). The output includes columns for the file system device, total size, used space, available space, use percentage, and mount point. For example, /dev/sda1 100G 60G 40G 60% / means the root partition is 60% full.

To check a specific mount point, provide the path: df -h /home. To see inode usage (which can also fill up), add the -i flag: df -i. Inodes store file metadata; running out of inodes prevents creating new files even if space remains.

The du Command: Directory Sizes

du (disk usage) estimates the space consumed by files and directories. To see the size of the current directory and its subdirectories, run:

du -h

This lists every subdirectory recursively, which can be overwhelming. To get a summary of a specific directory, use -s (summarize):

du -sh /var/log

The -h flag again gives human-readable output. To sort the results so the largest directories appear first, pipe to sort:

du -h /var | sort -hr | head -10

This shows the top 10 space consumers under /var. Note that du counts used blocks, so it may differ from the apparent file sizes shown by ls.

Quick Checks: ls and fdisk

For a single file or directory, ls -lh shows the size of files in human-readable format. For example, ls -lh /var/log/syslog reveals a 2.3M log file. This is useful for spotting a single large file.

To see partition layout and sizes, use fdisk -l (requires root privileges). It displays the disk device, sector size, and partition start/end sectors. While it does not show used space, it helps identify which physical disk corresponds to a mount point. For a more detailed view, lsblk shows the tree of block devices with sizes and mount points.

Finding Large Files with find

When disk space disappears, you often need to locate the culprit files. The find command can search for files larger than a given size. For example, to find files over 500 MB in the home directory:

find /home -type f -size +500M -exec ls -lh {} \;

This lists each large file with its size. Replace /home with the directory you want to scan. The -size +500M option matches files larger than 500 MB. You can use k for kilobytes, G for gigabytes, etc.

Interactive Tool: ncdu

For a more user-friendly experience, install ncdu (NCurses Disk Usage). It provides an interactive, navigable interface showing directory sizes. Run ncdu / to scan the entire file system. You can browse with arrow keys, delete files directly (press d), and sort by size. It is not installed by default on most distributions; install it with your package manager, e.g., sudo apt install ncdu on Debian/Ubuntu.

Common Problems and Misconceptions

One common issue is that df shows 100% usage but du does not account for all the space. This often happens because deleted files are still held open by a process. On Linux, deleting a file does not free the space until all processes close it. To find such files, use lsof +L1 (list open files with link count zero) and restart the responsible service.

Another misconception is that df and du should match. They measure different things: df reports the file system’s free blocks, while du sums the sizes of files. Reserved blocks (usually 5% for root) and metadata also create discrepancies.

Also, be aware that du can be slow on large directories because it reads every file’s metadata. Using --max-depth=1 limits recursion and speeds things up.

Frequently Asked Questions

How do I check disk space in Linux without df?

If df is not available (e.g., in a minimal environment), you can use cat /proc/mounts to see mounted file systems and stat -f / to get file system statistics like block counts and free blocks. Alternatively, use lsblk to see partition sizes, though it does not show usage.

What is the difference between df -h and df -k?

-h prints sizes in human-readable units (e.g., 1.5G), while -k forces sizes in 1K blocks. The latter is useful for scripts that need consistent numeric output. You can also use --block-size=1M to get megabytes.

Why does du show a directory size larger than the disk capacity?

This can happen if the directory contains hard links (each link counts as a separate entry in du by default) or if the file system uses compression or snapshots. Use du -l to count hard links only once, and remember that du reports apparent size unless you use --apparent-size.

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

Use SSH to run the same commands remotely: ssh user@server 'df -h'. For a more interactive session, ssh user@server ncdu / will launch the ncdu interface over the SSH connection.

What does the “Use%” column in df mean?

It shows the percentage of the file system’s total space that is currently used. For example, 60% means 60% of the partition is occupied. If it reaches 100%, the file system is full and you cannot write new files until space is freed.

Can I check disk space for a specific user?

There is no direct command, but you can use du -sh /home/username to see the total size of a user’s home directory. For a breakdown by subdirectory, use du -h --max-depth=1 /home/username. This is often sufficient for quota management.

Related

General steps for current mainstream versions; menus can differ by device and version. See the disclaimer or report an error.

More in this bed

The whole Tech How-To bed →