Quick Reference

Cheatsheets

Practical command references for Linux, networking, servers, containers, databases, and more.

Cheatsheet#linux-disk-management

Linux Disk & Storage Management Cheatsheet

Managing disks is a critical sysadmin task. This guide covers viewing disk space, partitioning, formatting, and mounting file systems.

Viewing Disk Space and Usage

df -h                     # Show file system disk space usage (human-readable)
df -T                     # Show file system types (ext4, xfs, etc.)
du -sh /path/to/dir       # Estimate directory space usage (summary, human-readable)
du -sh * | sort -hr       # Show size of all files/folders in current dir, sorted by size
lsblk                     # List all block devices (disks and partitions)
lsblk -f                  # List block devices with their UUIDs and file systems
fdisk -l                  # List all partition tables

Partitioning Disks

Warning: Partitioning and formatting will destroy data on the target disk. Be absolutely sure of the disk name (/dev/sdb, etc.).

Using fdisk (MBR / DOS)

sudo fdisk /dev/sdb
# Inside fdisk:
# m - Help
# n - New partition
# p - Print partition table
# w - Write changes and exit
# q - Quit without saving

Using parted (GPT - Good for disks > 2TB)

sudo parted /dev/sdb
# Inside parted:
# mklabel gpt             # Create a new GPT partition table
# mkpart primary ext4 0% 100% # Create a partition using 100% of the disk
# print                   # Show partitions
# quit                    # Exit

Formatting Partitions (Creating File Systems)

Once a partition (e.g., /dev/sdb1) is created, it must be formatted.

sudo mkfs.ext4 /dev/sdb1  # Format as ext4 (Standard Linux)
sudo mkfs.xfs /dev/sdb1   # Format as XFS (Good for large files, default on RHEL)
sudo mkfs.vfat /dev/sdb1  # Format as FAT32 (Compatible with Windows/Mac)
sudo mkfs.ntfs /dev/sdb1  # Format as NTFS (Windows)

Mounting and Unmounting

A formatted partition must be mounted to a directory to be accessible.

sudo mkdir -p /mnt/data            # Create a mount point
sudo mount /dev/sdb1 /mnt/data     # Mount the partition
sudo umount /mnt/data              # Unmount the partition (or umount /dev/sdb1)

Persistent Mounting (/etc/fstab)

To mount a drive automatically on boot, add it to /etc/fstab.

  1. Find the UUID of the partition:
sudo blkid /dev/sdb1
# Output: /dev/sdb1: UUID="1234abcd-..." TYPE="ext4"
  1. Edit /etc/fstab and add a new line:
# <file system> <mount point> <type> <options> <dump> <pass>
UUID=1234abcd-... /mnt/data ext4 defaults 0 2
  1. Test the fstab configuration (Critical! Prevents boot failures):
sudo mount -a