Lesson 12: Disk & Storage

Goal: Learn to inspect disk usage, identify space hogs, and manage storage.


Table of Contents

  1. Filesystem Overview — df
  2. Directory Size — du
  3. Interactive Disk Usage — ncdu
  4. Block Devices — lsblk
  5. Mounting & Unmounting
  6. Partitions — fdisk
  7. Practical: Finding Space Hogs
  8. Exercises

Filesystem Overview — df

df (disk free) shows how much space is used on each mounted filesystem.

df -h

Output:

Filesystem      Size  Used Avail Use% Mounted on
/dev/sda1        50G   22G   26G  46% /
/dev/sda2       200G  150G   40G  79% /home
tmpfs           7.8G  1.2M  7.8G   1% /tmp
/dev/sdb1       1.0T  650G  350G  65% /data

Useful flags

# Human-readable sizes
df -h

# Show filesystem type
df -Th

# Show only specific filesystem
df -h /home

# Show only local filesystems (no network mounts)
df -hl

# Inode usage (number of files, not space)
df -i

Understanding the output

Column Meaning
Filesystem Device or partition
Size Total size
Used Space used
Avail Space available
Use% Percentage used
Mounted on Where it's accessible

Warning level: Keep an eye on partitions above 80%. At 100%, the system can become unstable.


Directory Size — du

du (disk usage) shows how much space files and directories use.

Size of a directory

# Total size of a directory
du -sh /var/log

# Output: 450M    /var/log

Size per subdirectory

# One level deep
du -h --max-depth=1 /home/benjamin

# Output:
# 1.2G    /home/benjamin/Documents
# 3.4G    /home/benjamin/Downloads
# 256M    /home/benjamin/.config
# 5.1G    /home/benjamin

Sort by size

# Largest directories first
du -h --max-depth=1 /home/benjamin | sort -rh

# Top 10 largest directories
du -h --max-depth=1 / 2>/dev/null | sort -rh | head -10

Useful flags

# Summary only
du -sh folder/

# Include files (not just directories)
du -ah folder/ | sort -rh | head -20

# Exclude patterns
du -h --exclude='*.log' /var/

# Total of multiple paths
du -ch /var/log /var/cache

Interactive Disk Usage — ncdu

ncdu is an interactive, visual disk usage analyzer. Much easier than du.

Install

sudo apt install ncdu

Usage

# Analyze current directory
ncdu

# Analyze a specific path
ncdu /var

# Analyze home directory
ncdu ~
Key Action
Arrow keys Navigate
Enter Enter directory
d Delete selected item
n Sort by name
s Sort by size
g Toggle graph
q Quit

Example output

ncdu 1.17 ~ Use the arrow keys to navigate, press ? for help
--- /home/benjamin ----
    3.4 GiB [##########] /Downloads
    1.2 GiB [###       ] /Documents
  256.0 MiB [          ] /.config
  128.0 MiB [          ] /.local
   45.0 MiB [          ] /Pictures
   12.0 MiB [          ] /.cache

Block Devices — lsblk

lsblk lists all block devices (disks, partitions, USB drives).

lsblk

Output:

NAME   MAJ:MIN RM   SIZE RO TYPE MOUNTPOINT
sda      8:0    0   250G  0 disk
├─sda1   8:1    0    50G  0 part /
├─sda2   8:2    0   196G  0 part /home
└─sda3   8:3    0     4G  0 part [SWAP]
sdb      8:16   0     1T  0 disk
└─sdb1   8:17   0     1T  0 part /data
sr0     11:0    1  1024M  0 rom

With filesystem info

lsblk -f

Output:

NAME   FSTYPE LABEL  UUID                                 MOUNTPOINT
sda
├─sda1 ext4          a1b2c3d4-...                        /
├─sda2 ext4          e5f6g7h8-...                        /home
└─sda3 swap          i9j0k1l2-...                        [SWAP]

Mounting & Unmounting

Mount a device

# Create a mount point
sudo mkdir /mnt/usb

# Mount a USB drive
sudo mount /dev/sdb1 /mnt/usb

# Mount with specific filesystem type
sudo mount -t ext4 /dev/sdb1 /mnt/usb

# Mount read-only
sudo mount -o ro /dev/sdb1 /mnt/usb

Unmount

sudo umount /mnt/usb

# Force unmount (if busy)
sudo umount -f /mnt/usb

# Lazy unmount (detach and clean up later)
sudo umount -l /mnt/usb

Check what's mounted

mount | grep "^/dev"
# or
findmnt

Permanent mounts — /etc/fstab

cat /etc/fstab

Format:

# device         mount-point  type  options    dump  pass
/dev/sda1        /            ext4  defaults   0     1
/dev/sda2        /home        ext4  defaults   0     2
/dev/sdb1        /data        ext4  defaults   0     2

To add a permanent mount:

# 1. Get the UUID
sudo blkid /dev/sdb1

# 2. Add to fstab
echo "UUID=your-uuid-here /data ext4 defaults 0 2" | sudo tee -a /etc/fstab

# 3. Test (mount all fstab entries)
sudo mount -a

Partitions — fdisk

# List all partitions
sudo fdisk -l

# Interactive partition editor (careful!)
sudo fdisk /dev/sdb

fdisk can destroy data. Only use it if you know what you're doing, and always back up first.


Practical: Finding Space Hogs

Quick commands for finding large files

# Top 20 largest files on the system
sudo find / -type f -exec du -h {} + 2>/dev/null | sort -rh | head -20

# Top 10 largest directories in /home
du -h --max-depth=2 /home 2>/dev/null | sort -rh | head -10

# Find files larger than 500 MB
find / -type f -size +500M -exec ls -lh {} \; 2>/dev/null

# Find old log files
find /var/log -name "*.gz" -mtime +30 -exec ls -lh {} \;

Common space wasters

# Package cache
sudo du -sh /var/cache/apt/
sudo apt clean    # free it up

# Old kernels
dpkg --list 'linux-image-*' | grep '^ii'

# Docker images
docker system df
docker system prune -a    # clean up

# Journal logs
sudo journalctl --disk-usage
sudo journalctl --vacuum-size=100M    # limit to 100 MB

Exercises

Exercise 1: Check disk space

# 1. Show filesystem usage
df -h

# 2. Show usage of your home partition
df -h /home

# 3. What percentage is used on /?
df -h / | awk 'NR==2 {print $5}'

Exercise 2: Find large directories

# 1. Top 5 largest directories in /var
sudo du -h --max-depth=1 /var 2>/dev/null | sort -rh | head -5

# 2. Size of your home directory
du -sh ~

# 3. Size per subdirectory in your home
du -h --max-depth=1 ~ | sort -rh

Exercise 3: Interactive analysis

# 1. Install ncdu if needed
sudo apt install ncdu

# 2. Analyze /var
sudo ncdu /var

# 3. Navigate to find the largest directories
# Press 'q' to exit

Key Takeaways

  • df -h — filesystem space overview (check Use%)
  • du -sh folder/ — size of a specific directory
  • du -h --max-depth=1 | sort -rh — find large subdirectories
  • ncdu — interactive, visual disk analyzer
  • lsblk — list disks and partitions
  • mount / umount — attach/detach filesystems
  • Regularly check /var/log, package caches, and Docker for space waste

Next Lesson: Lesson 13: Package Management →