Smart Systems for Home

Plain how-to guides for smart home, security and home Wi-Fi

Check Linux Disk Usage of Files and Directories

· 5 min read · Home Tech

In this article
  1. Background: Why Disk Usage Matters
  2. The Core Commands: du and df
  3. How du Works and Its Key Options
  4. Practical Examples: Finding Large Files and Directories
  5. Common Pitfalls and Misconceptions
  6. Advanced Tools: ncdu and Graphical Alternatives
  7. Safety and Performance Considerations
  8. Frequently Asked Questions

To check disk usage of files and directories on Linux, use the du command (disk usage) for directory and file sizes, and df (disk free) for overall filesystem capacity. The most common invocation is du -sh /path/to/directory, which shows a human-readable total size for that path.

Background: Why Disk Usage Matters

Linux systems store data in a hierarchical filesystem, and disk space is a finite resource that fills up faster than most users expect. Log files, package caches, virtual machine images, and database files can grow silently, eventually causing applications to fail or the system to become unstable. Knowing exactly which files and directories consume the most space is essential for routine maintenance, planning capacity, and troubleshooting "disk full" errors. The standard tools for this purpose have existed since the early days of Unix and remain unchanged in modern distributions.

The Core Commands: du and df

Two commands form the foundation of disk usage analysis:

  • du — estimates file and directory space usage. It traverses the directory tree and reports the sum of all file sizes within a given path.
  • df — reports the amount of disk space used and available on mounted filesystems. It shows the big picture: total capacity, used space, free space, and mount points.

For most tasks, you will use du to find large directories and df to confirm which filesystem is full. For example, df -h displays all mounted filesystems with sizes in human-readable format (KB, MB, GB).

How du Works and Its Key Options

The du command calculates the apparent size of files plus the disk blocks allocated to them. By default, it counts each file once, even if the file is hard-linked (multiple names pointing to the same data). The most useful options are:

  • -h — human-readable output (e.g., 4.0K, 12M, 3.5G).
  • -s — summary; only show the total for each argument, not every subdirectory.
  • -a — show sizes for all files, not just directories.
  • -c — produce a grand total at the end.
  • --max-depth=N — limit the depth of directory traversal (e.g., --max-depth=1 shows only immediate subdirectories).
  • -x — skip directories on different filesystems (useful to avoid counting mounted drives).

For example, du -h --max-depth=1 /var lists the sizes of all direct subdirectories of /var, making it easy to spot a log directory that has grown to several gigabytes.

Practical Examples: Finding Large Files and Directories

Here are common real-world scenarios and the commands to solve them:

  1. Find the largest directories in the current directory: du -h --max-depth=1 | sort -hr — this sorts the output by size, descending.
  2. Find the largest files anywhere under a directory: du -ah /path | sort -hr | head -20 — lists the 20 largest files or directories.
  3. Check if a specific directory is too large: du -sh /home/user — gives a single line total.
  4. See how much space is free on the disk: df -h — shows all mounted filesystems.

These commands work on any Linux distribution (Ubuntu, Debian, Fedora, Arch, etc.) without installing extra software.

Common Pitfalls and Misconceptions

Several misunderstandings lead to incorrect conclusions when checking disk usage:

  • du vs. ls file sizes: ls -l shows the apparent file size, while du shows the actual disk blocks used. For small files, du may report more than the file size because of filesystem block allocation (e.g., a 1-byte file may occupy 4KB on disk).
  • Hard links are counted multiple times by default: If two hard links point to the same inode, du counts the space twice. Use -l to count hard links only once.
  • Hidden files are included: du includes dotfiles (like .cache), which often consume significant space.
  • Permission errors: When run as a normal user, du may not be able to read some directories (e.g., other users' home folders). Use sudo to get a complete picture.

Advanced Tools: ncdu and Graphical Alternatives

While du is powerful, its text output is not always the most convenient for interactive exploration. The ncdu (NCurses Disk Usage) tool provides an interactive, navigable interface. Install it with your package manager (e.g., sudo apt install ncdu on Debian/Ubuntu, sudo dnf install ncdu on Fedora). Once installed, run ncdu /path to browse directories, sort by size, and delete files directly from the interface. For users who prefer a graphical view, tools like Baobab (GNOME Disk Usage Analyzer) or QDirStat offer a visual representation of disk usage.

Safety and Performance Considerations

Running du on a large filesystem (e.g., /) can take a long time and consume significant I/O. It is best to run it during off-peak hours or limit the scope to a specific directory. Also, be careful when deleting files based on du output — always verify that the file or directory is not required by the system or an application. For example, deleting /var/log files without checking may remove logs needed for troubleshooting. Use du as a diagnostic tool, not as a reason to delete blindly.

Frequently Asked Questions

What is the difference between du and df?

du estimates the space used by files and directories within a filesystem, while df reports the total space on the entire filesystem, including used and available capacity. df answers "How full is my disk?" while du answers "Which folder is taking up space?"

Why does du show a different size than ls -l?

ls -l shows the logical file size (the number of bytes in the file), while du shows the actual disk usage, which includes filesystem overhead and block allocation. For example, a file of 1 byte may occupy 4096 bytes (one block) on disk, so du reports 4.0K.

How do I check disk usage of a specific file?

Run du -h filename to see the disk usage of a single file. For multiple files, use du -h file1 file2 or du -ch *.log to get a total.

How do I find the largest directories in my home folder?

Use du -h --max-depth=1 ~ | sort -hr to list the immediate subdirectories of your home folder sorted by size. For a more interactive view, install and run ncdu ~.

Does du count files on other mounted filesystems?

By default, du traverses into mounted filesystems (e.g., external drives) if they are under the path you specify. Use the -x option to stay on the same filesystem and avoid counting mounted devices.

Why does du take so long on my system?

Because du must read every directory entry and file metadata in the tree. On large filesystems with millions of files, this can take minutes. Use --max-depth or target specific directories to speed it up.