Skip to main content

fdisk Cheatsheet

By Dejan Panovski Updated on Download PDF

Quick reference for fdisk commands: list disks, create partition tables, add partitions, change partition types, write changes, and quit safely

fdisk is a menu-driven Linux tool for creating and managing disk partition tables. This cheatsheet covers the command-line options and interactive commands you need to list disks, create GPT or MBR tables, add partitions, change partition types, and save or discard changes.

Basic Usage

Open a disk or print partition tables.

CommandDescription
sudo fdisk /dev/sdXOpen a disk in interactive mode
sudo fdisk -lList all detected partition tables
sudo fdisk -l /dev/sdXList one disk partition table
sudo fdisk --helpShow command-line options
sudo fdisk --versionShow fdisk version

List Disks

Identify the correct device before changing partitions.

CommandDescription
lsblkShow disks, partitions, and mount points
lsblk -d -o NAME,SIZE,MODELShow whole disks only
lsblk -fShow filesystems and UUIDs
sudo fdisk -lShow partition tables with disk labels
sudo fdisk -x /dev/sdXShow detailed partition information

Interactive Commands

Commands used inside the fdisk prompt.

CommandDescription
mShow help menu
pPrint the current partition table
nCreate a new partition
dDelete a partition
tChange partition type
lList available partition types
wWrite changes and exit
qQuit without saving

Partition Tables

Create a new disk label before adding partitions on a blank disk.

CommandDescription
gCreate a new GPT partition table
oCreate a new MBR (DOS) partition table
pReview the current table before saving
wWrite the new table to disk
qExit without writing changes

Create Partitions

Common answers while creating a new partition with n.

InputDescription
nStart a new partition
EnterAccept the default partition number
EnterAccept the default first sector
+1GCreate a 1 GiB partition
+100GCreate a 100 GiB partition
EnterUse the rest of the available disk space
pPrint the proposed layout
wSave the changes

Partition Types

Set the partition type when the partition is not a regular Linux data partition.

InputDescription
lList partition types
tChange a partition type
Linux filesystemRegular Linux data partition (GPT)
Linux swapSwap partition (GPT)
EFI SystemEFI System partition (GPT)
Linux LVMLVM physical volume (GPT)
Linux RAIDLinux RAID member (GPT)
83Regular Linux partition (MBR)
82Swap partition (MBR)
8eLVM partition (MBR)

Review and Save

Check the in-memory table before writing it to disk.

CommandDescription
pPrint the pending partition table
vVerify the partition table
iShow details about a partition
wWrite changes to disk and exit
qQuit without saving changes

Format and Mount

After writing the partition table, create a filesystem and mount the partition.

CommandDescription
sudo mkfs.ext4 /dev/sdX1Format a partition as ext4
sudo mkswap /dev/sdX2Create swap on a partition
sudo swapon /dev/sdX2Enable swap
sudo mkdir -p /mnt/dataCreate a mount point
sudo mount /dev/sdX1 /mnt/dataMount the partition
lsblk -fConfirm filesystem and mount details

Safety Checks

Commands that help avoid editing the wrong disk.

CommandDescription
lsblk -d -o NAME,SIZE,MODELCompare disk names, sizes, and models
lsblk -fCheck existing filesystems and mount points
sudo fdisk -l /dev/sdXReview the current table before editing
mount | grep /dev/sdXCheck whether partitions are mounted
sudo umount /dev/sdX1Unmount a partition before changing it
sudo partprobe /dev/sdXAsk the kernel to re-read the table

References for the full workflow around disks and partitions.

ToolDescription
mountMount and unmount filesystems
mkfs.ext4Format a partition with a filesystem
dfCheck filesystem disk usage
fsckCheck and repair filesystems