fdisk Cheatsheet
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.
| Command | Description |
|---|---|
sudo fdisk /dev/sdX | Open a disk in interactive mode |
sudo fdisk -l | List all detected partition tables |
sudo fdisk -l /dev/sdX | List one disk partition table |
sudo fdisk --help | Show command-line options |
sudo fdisk --version | Show fdisk version |
List Disks
Identify the correct device before changing partitions.
| Command | Description |
|---|---|
lsblk | Show disks, partitions, and mount points |
lsblk -d -o NAME,SIZE,MODEL | Show whole disks only |
lsblk -f | Show filesystems and UUIDs |
sudo fdisk -l | Show partition tables with disk labels |
sudo fdisk -x /dev/sdX | Show detailed partition information |
Interactive Commands
Commands used inside the fdisk prompt.
| Command | Description |
|---|---|
m | Show help menu |
p | Print the current partition table |
n | Create a new partition |
d | Delete a partition |
t | Change partition type |
l | List available partition types |
w | Write changes and exit |
q | Quit without saving |
Partition Tables
Create a new disk label before adding partitions on a blank disk.
| Command | Description |
|---|---|
g | Create a new GPT partition table |
o | Create a new MBR (DOS) partition table |
p | Review the current table before saving |
w | Write the new table to disk |
q | Exit without writing changes |
Create Partitions
Common answers while creating a new partition with n.
| Input | Description |
|---|---|
n | Start a new partition |
Enter | Accept the default partition number |
Enter | Accept the default first sector |
+1G | Create a 1 GiB partition |
+100G | Create a 100 GiB partition |
Enter | Use the rest of the available disk space |
p | Print the proposed layout |
w | Save the changes |
Partition Types
Set the partition type when the partition is not a regular Linux data partition.
| Input | Description |
|---|---|
l | List partition types |
t | Change a partition type |
Linux filesystem | Regular Linux data partition (GPT) |
Linux swap | Swap partition (GPT) |
EFI System | EFI System partition (GPT) |
Linux LVM | LVM physical volume (GPT) |
Linux RAID | Linux RAID member (GPT) |
83 | Regular Linux partition (MBR) |
82 | Swap partition (MBR) |
8e | LVM partition (MBR) |
Review and Save
Check the in-memory table before writing it to disk.
| Command | Description |
|---|---|
p | Print the pending partition table |
v | Verify the partition table |
i | Show details about a partition |
w | Write changes to disk and exit |
q | Quit without saving changes |
Format and Mount
After writing the partition table, create a filesystem and mount the partition.
| Command | Description |
|---|---|
sudo mkfs.ext4 /dev/sdX1 | Format a partition as ext4 |
sudo mkswap /dev/sdX2 | Create swap on a partition |
sudo swapon /dev/sdX2 | Enable swap |
sudo mkdir -p /mnt/data | Create a mount point |
sudo mount /dev/sdX1 /mnt/data | Mount the partition |
lsblk -f | Confirm filesystem and mount details |
Safety Checks
Commands that help avoid editing the wrong disk.
| Command | Description |
|---|---|
lsblk -d -o NAME,SIZE,MODEL | Compare disk names, sizes, and models |
lsblk -f | Check existing filesystems and mount points |
sudo fdisk -l /dev/sdX | Review the current table before editing |
mount | grep /dev/sdX | Check whether partitions are mounted |
sudo umount /dev/sdX1 | Unmount a partition before changing it |
sudo partprobe /dev/sdX | Ask the kernel to re-read the table |