Chapter 30: Bash Directory Usage (du)

What does du actually do? (super simple first)

du = disk usage It tells you how much space each folder (and its contents) is using on your disk.

Key difference from df:

  • df → shows total/used/available per partition/drive (big picture)
  • du → shows how much space each folder/file/subfolder is using (detailed breakdown)

Without du, you see /home is 200 GB full in df, but you don’t know which folder inside /home is the big culprit (Downloads? Videos? .cache? node_modules?).

du helps you hunt down space hogs so you can clean up safely.

1. Basic usage (try right now!)

Bash

This shows space used by every folder in your home directory (and subfolders) in 1K-blocks (1024 bytes each) – very hard to read!

Better – always use these flags together:

Bash
  • -s = summary (only show total for the folder you asked)
  • -h = human-readable (shows KB, MB, GB, TB instead of blocks)

Example output you might see:

text

2. Most useful combinations (you’ll use these 95% of time)

What you want to see Command you type What it shows
Total space used by your home folder du -sh ~ or du -sh $HOME One nice line (e.g. 48G)
Biggest folders in current directory du -sh * sort -hr
Top 10 biggest folders in /home du -sh /home/* sort -hr
Detailed breakdown of one folder du -h –max-depth=1 ~/.cache sort -hr
Find huge files (not folders) du -ah ~ sort -hr
Check entire root (careful – takes time) sudo du -sh /* sort -hr
Ignore permission errors sudo du -shx /* 2>/dev/null sort -hr

Try this right now (very useful!):

Bash

Example output:

text

Now you know exactly where to clean first!

3. Very common flags explained (learn these!)

Flag Meaning When to use
-h Human-readable (GB, MB, etc.) Always!
-s Summary – only total Quick total check
-a All – show files too (not just folders) Find big single files
–max-depth=N Limit depth of display Avoid thousands of lines
-c Grand total at bottom When listing many folders
-x Stay on one filesystem (skip mounted drives) Faster on /
–apparent-size Show apparent size (not disk blocks) When symlinks/hard links confuse
-k / -m Force KB / MB output Old scripts

4. Real-life examples (Hyderabad developer style)

  • Laptop saying disk full when downloading movies?
    Bash
  • VS Code / Node.js project eating space?
    Bash
  • Where are all my Python virtualenvs?
    Bash
  • Check Docker images / containers / volumes
    Bash
  • Find biggest log files
    Bash
  • Quick space check before big git clone
    Bash

5. Quick cheat-sheet table

Goal Command example Notes
Total home usage du -sh ~ Fastest check
Top 10 biggest things in current dir du -sh * sort -hr
Biggest folders in /home sudo du -sh /home/* sort -hr
Detailed 1-level deep du -h --max-depth=1 ~/.cache sort -hr
All files + folders sorted du -ah ~ sort -hr
Skip mounted drives (faster on /) sudo du -shx /* sort -hr
Total + list du -sch * sort -hr
Find old large backups du -sh ~/backup_* sort -hr`

6. Pro tips from daily use

  • du is slow on huge folders (e.g. millions of files) – be patient or use –max-depth
  • Combine with sort -hr almost always (human-readable + reverse + numeric)
  • Use sudo when checking /var, /usr, /opt
  • du shows disk blocks used (can be more than file size due to block size) → use –apparent-size if you want actual file size
  • Alternative modern tools: ncdu (install it – interactive ncurses du!)
    Bash

Now open your terminal and try these 3 right now:

Bash

Tell me:

  • What’s the biggest folder in your home right now? (e.g. 28G Videos)
  • Or ask: “How to find all files bigger than 1GB?” or “du vs df – when to use which?” or “How to use ncdu?”

We’ll solve it together! 😄

You may also like...

Leave a Reply

Your email address will not be published. Required fields are marked *