💽
Disks & storage cheat sheet
Commands for finding what is using disk space and for working with block devices. Commands that write to devices are marked as destructive; always double-check the device name.
Click any command to copy it.
Disk space
| Command | What it does |
|---|---|
df -h | Free and used space per filesystem. |
df -i | Inode usage (a full inode table looks like a full disk). |
du -sh dir | Total size of a directory. |
du -h --max-depth=1 . | sort -h | Size of each subdirectory, smallest to largest. |
du -ah . | sort -rh | head -20 | The 20 biggest files and folders below here. |
ncdu / | Interactive disk usage browser (install ncdu first). |
sudo lsof +L1 | Deleted files still held open, a common cause of 'missing' space. |
Devices and filesystems
| Command | What it does |
|---|---|
lsblk | List block devices as a tree. |
lsblk -f | Include filesystem type, label and UUID. |
sudo blkid | Show UUIDs and types of devices. |
sudo fdisk -l | List disks and partitions. |
findmnt | Show mounted filesystems as a tree. |
sudo smartctl -a /dev/sda | Show SMART health data (install smartmontools). |
Mount
| Command | What it does |
|---|---|
sudo mount /dev/sdb1 /mnt | Mount a partition. |
sudo umount /mnt | Unmount it. |
sudo mount -o ro /dev/sdb1 /mnt | Mount read-only. |
UUID=xxxx /data ext4 defaults,nofail 0 2 | Example /etc/fstab line for mounting at boot. |
sudo mount -a | Mount everything in fstab (a good test for typos). |
sudo umount -l /mnt | Lazy unmount of a busy filesystem. |
Format and repair
| Command | What it does |
|---|---|
sudo mkfs.ext4 /dev/sdb1 | Destructive: Create a new ext4 filesystem, destroying all data on the partition. |
sudo fsck /dev/sdb1 | Check and repair a filesystem (unmount it first). |
sudo wipefs -a /dev/sdb | Destructive: Erase filesystem signatures from a device. |
sudo dd if=image.iso of=/dev/sdX bs=4M status=progress | Destructive: Write an image directly to a device. A wrong device name destroys data. |
sudo resize2fs /dev/sdb1 | Grow an ext4 filesystem to fill its partition. |
LVM
| Command | What it does |
|---|---|
sudo pvs; sudo vgs; sudo lvs | Summarise physical volumes, volume groups and logical volumes. |
sudo lvextend -r -L +10G /dev/vg/lv | Grow a logical volume by 10 GB and resize its filesystem. |
sudo lvcreate -L 20G -n data vg | Create a 20 GB logical volume. |
iostat -x 1 | Per-device I/O statistics every second (install sysstat). |
Frequently asked questions
My disk is full but du shows little. Why?
Deleted files held open by a running process still use space (check lsof +L1), or you may have run out of inodes (check df -i).
How do I find the biggest directories?
Run du -h --max-depth=1 . | sort -h from the top and keep drilling into the largest entry, or use ncdu.