Smart Systems for Home

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

Find Top Large Directories and Files Sizes in Linux

· 5 min read · Home Tech

In this article
  1. Why Disk Usage Analysis Matters
  2. Using du to Find Large Directories
  3. Using find to Locate Large Files
  4. Combining du and find for a Complete Picture
  5. Sorting and Formatting Options
  6. Common Pitfalls and Permission Issues
  7. Alternatives: ncdu and Graphical Tools
  8. Frequently Asked Questions

To find the largest files and directories in Linux, use the du command combined with sort and head, or the find command with -printf for files. For example, du -ah /path | sort -rh | head -20 lists the 20 largest items, and find /path -type f -printf '%s %p\n' | sort -rn | head -20 lists the 20 largest files.

Why Disk Usage Analysis Matters

Linux systems accumulate large files and directories over time—log files, database dumps, virtual machine images, caches, and user downloads. When a disk fills up, applications fail, services crash, and system performance degrades. Identifying what consumes space is the first step in cleanup, capacity planning, and preventing outages. The built-in tools du (disk usage) and find are available on virtually every Linux distribution, making them the standard approach for this task.

Using du to Find Large Directories

The du command estimates file space usage. Its key options:

  • -a: show files and directories (by default only directories)
  • -h: human-readable sizes (K, M, G)
  • -s: summary only (total for each argument)
  • -x: stay on one filesystem (skip mounted volumes)
  • --max-depth=N: limit recursion depth

To find the largest directories in the current directory, run:

du -h --max-depth=1 | sort -hr | head -20

This shows the total size of each subdirectory, sorted from largest to smallest. For a whole filesystem, start at the root (/) but consider using -x to avoid scanning mounted devices like /proc, /sys, or external drives.

Using find to Locate Large Files

While du covers directories, find excels at pinpointing individual files. The -printf action prints file attributes in a custom format. To list files larger than 100 MB:

find / -type f -size +100M -printf '%s %p\n' 2>/dev/null | sort -rn | head -20

Explanation:

  • -type f: only regular files
  • -size +100M: files larger than 100 MiB (use c for bytes, k for KiB, G for GiB)
  • -printf '%s %p\n': print size in bytes, then path
  • sort -rn: sort numerically, reverse (largest first)
  • head -20: show the top 20

Redirect stderr to /dev/null to suppress permission errors. To include human-readable sizes, use du -ah on a directory tree instead.

Combining du and find for a Complete Picture

For a thorough analysis, start with du to identify large directories, then drill into each with find to see the files responsible. For example:

  1. Run du -h --max-depth=2 /var | sort -hr | head -20 to spot large subdirectories.
  2. For a suspect directory like /var/log, run find /var/log -type f -printf '%s %p\n' | sort -rn | head -10.
  3. Delete or archive files as appropriate, then re-run du to confirm freed space.

This two-step approach is more efficient than scanning the entire filesystem repeatedly.

Sorting and Formatting Options

Sorting is critical. sort -h handles human-readable sizes (e.g., 1K, 2M, 3G) correctly, while sort -n sorts by numeric value (bytes). Use -r for reverse order (largest first). If you prefer a custom output, du can print sizes in bytes with --block-size=1, then pipe to numfmt for human-readable formatting:

du --block-size=1 -s * | sort -n | tail -10 | numfmt --to=iec

This lists the 10 largest items in the current directory with IEC units (KiB, MiB).

Common Pitfalls and Permission Issues

Running as a regular user, you may see Permission denied errors when scanning system directories. Use sudo to read all files, or redirect errors as shown. Another pitfall: du counts hard links multiple times unless you use -l (count links each time) or --count-links; by default it counts each hard link once, which can under-report. Also, sparse files (files with holes) may show a smaller size than their apparent length; du reports actual disk usage, while find -printf '%s' shows logical size. For filesystems with compression, du reflects compressed size, which may differ from the file's logical size.

Alternatives: ncdu and Graphical Tools

For interactive exploration, ncdu (NCurses Disk Usage) is a popular terminal tool. Install it with your package manager (apt install ncdu, dnf install ncdu, etc.). It provides a navigable interface showing sizes and allows deleting files directly. Graphical tools like baobab (GNOME Disk Usage Analyzer) or kdirstat offer a visual representation. These are not standard on minimal systems but are convenient for desktop users.

Frequently Asked Questions

How do I find the largest files in a specific directory?

Use find /path/to/dir -type f -printf '%s %p\n' | sort -rn | head -10. Replace the path and adjust the number of results. For human-readable sizes, pipe through numfmt or use du -ah /path | sort -rh | head -10.

Why does du show a directory size larger than the sum of its files?

Directories contain metadata (inodes, directory entries) that consume space, and files may have multiple hard links. Also, du by default counts each file once, but if a file appears in multiple directories via hard links, it may be counted multiple times unless you use --count-links (which counts each link separately) or the default (which counts only once).

How can I exclude certain directories when searching?

Use find with -not -path or -prune. For example, to exclude /proc and /sys: find / -path /proc -prune -o -path /sys -prune -o -type f -printf '%s %p\n' 2>/dev/null | sort -rn | head -20. For du, use --exclude (e.g., du -h --exclude=/proc --max-depth=1 /).

What is the difference between du and df?

du estimates file and directory space usage, while df reports free and used space on mounted filesystems. df -h shows overall disk usage, but it does not tell you which files consume space. Use both: df to see if a disk is full, then du to find what is using it.

Can I find large files by size range, like between 1GB and 5GB?

Yes, use find with multiple -size tests: find / -type f -size +1G -size -5G -printf '%s %p\n' 2>/dev/null | sort -rn | head -20. Note that -size uses 1024-byte blocks by default; use c for bytes, k for KiB, M for MiB, G for GiB.

Why does sort -h not work on some systems?

The -h option for sort is a GNU extension. On BSD/macOS systems, use sort -n with byte sizes, or install GNU coreutils. For a portable approach, always output sizes in bytes and sort numerically.