Back to graph

Page

How to Check Disk Size of a Directory On Linux

Page IDlinux-dir-size-checkUpdated

📖 Quick Reference

🎯 Goal 🛠️ Command
Total size (human‑readable) du -sh /path/to/dir
Show each sub‑directory (1‑level deep) du -h --max-depth=1 /path/to/dir
List all files & dirs du -ah /path/to/dir
Sort by size (largest first) du -h /path/to/dir \\| sort -hr \\| head -n 20
Exclude patterns du -sh --exclude='*.cache' /path/to/dir
Follow symlinks du -shL /path/to/dir
Grand total only du -shc /path/to/dir
Interactive UI ncdu /path/to/dir
Filesystem free space df -h /path/to/dir

🛠️ Core Tool: `du` (Disk Usage)

du walks a directory tree and reports the amount of disk blocks used.

⚙️ Frequently Used Flags

Flag Meaning
-s Summary – only total for each argument
-h Human‑readable – auto‑scale to KiB/MiB/GiB
-a All – list size for every file (not just dirs)
-c Grand total – add a final total line
-d N / --max-depth=N Limit recursion depth to N levels
-L Follow symbolic links (default: don’t)
--exclude=PATTERN Skip files/dirs matching PATTERN
-B SIZE Display sizes in bytes, KB, MB, … (e.g., -B1 for bytes)
--apparent-size Show logical file size, not allocated blocks
--one-file-system Stay on the same filesystem (ignore mounted sub‑volumes)

📏 Example: Simple Total Size

du -sh /var/log
# → 124M    /var/log

📂 Example: One‑Level Summary

du -h --max-depth=1 /var/log
# → 12M    /var/log/apache2
# → 8.4M   /var/log/mysql
# → 124M   /var/log

📂 Example: Full Tree (including files)

du -ah /var/log | less

📊 Example: Sort by Size (largest first)

du -h /var/log | sort -hr | head -n 20

📦 Example: Exclude Cache Directories

du -sh --exclude='*.cache' /home/user

🔗 Example: Follow Symbolic Links

du -shL /path/with/symlinks

🎛️ Advanced Tools & Techniques

🧭 `ncdu` – Interactive Disk Usage Explorer

# Install (Debian/Ubuntu)
sudo apt install ncdu

# Run
ncdu /home/user
  • Arrow keys → navigate

  • d → delete selected entry

  • q → quit

📂 `find` + `du` – Filtered Size Calculations

Size of only *.log files:

find /var/log -type f -name '*.log' -print0 |
  du --files0-from=- -ch |
  tail -1

Top 10 biggest files:

find /path/to/dir -type f -printf '%s %p\\\\n' |
  sort -nr |
  head -n 10 |
  numfmt --to=iec

📊 `df` – Check Free Space on the Underlying Filesystem

df -h /var/log
# Filesystem      Size  Used Avail Use% Mounted on
# /dev/sda1       100G   76G   20G  80% /

⚡ Tips & Tricks

  • Run as root (sudo) to avoid “permission denied” gaps.

  • Use **B1** for an exact byte count (du -sb is equivalent).

  • Combine du with awk for custom reports:

du -sk /home/* | awk '{printf "%s MB\\t%s\\n", $1/1024, $2}'


- To **ignore mounted filesystems** (e.g., network shares) add `-one-file-system`:
```Bash
du -sh --one-file-system /mnt/backup
  • Apparent size vs. allocated size: du --apparent-size -sh /path shows logical file size, while plain du shows space actually used on disk (including block padding, sparse file handling).

  • Exclude multiple patterns:

du -sh /path \\ --exclude='.tmp' \\ --exclude='cache/'


---

🚨 Common Pitfalls

❗ Issue 🔍 Why it Happens ✅ Fix
Reported size > actual file size du counts disk blocks; sparse files & block rounding inflate numbers Use du --apparent-size to see logical size
“Permission denied” lines hide data Non‑root user lacks read rights on some sub‑dirs Prefix with sudo or adjust ACLs
Symlinks double‑count Using -L makes du follow links, potentially counting the same data twice Omit -L unless you specifically need linked target size
Mounted sub‑volumes inflate result du traverses into other filesystems by default Add --one-file-system
Output not human‑readable Forgetting -h flag Always include -h (or -H for powers of 1000)

📌 Summary Cheat Sheet

Goal Command
One‑line total du -sh /path/to/dir
Depth‑limited view du -h --max-depth=N /path/to/dir
All files listed du -ah /path/to/dir
Sorted by size du -h /path/to/dir \\| sort -hr
Interactive UI ncdu /path/to/dir
Exclude pattern du -sh --exclude='*.cache' /path
Follow symlinks du -shL /path
Filesystem free space df -h /path

💡 Pro tip: Pair du -sh with df -h to see both used and available space at a glance.

Happy measuring! 🎉