fdisk cheatsheet:

fdisk is a powerful, text-based utility used to create, delete, and manage disk partitions in Linux systems. It supports MBR (Master Boot Record) partition tables and is best suited for systems not using GPT (GUID Partition Table).

Basic syntax

fdisk [options] /dev/sdX

Where /dev/sdX is the disk you want to operate on (e.g., /dev/sda, /dev/sdb).

Always double-check the disk name to avoid data loss.


Key commands in fdisk interactive mode

CommandDescription
mPrint help menu
pDisplay existing partition table
nCreate a new partition
dDelete a partition
tChange a partition’s system ID (type)
aToggle bootable flag
wWrite changes and exit
qQuit without saving changes

Common workflow examples

1. View partition table

sudo fdisk -l

Lists all disks and their partitions.

2. Start fdisk on a specific disk

sudo fdisk /dev/sdX

Enters interactive mode.

3. Create a new partition

Command (m for help): n
Select default or choose primary (p) or extended (e)
Partition number: 1
First sector: [Press Enter to accept default]
Last sector: +1G  # or specify size like +20G

4. Change partition type

Command (m for help): t
Partition number: 1
Hex code (type L for list): 83  # Linux filesystem

5. Set bootable flag

Command (m for help): a
Partition number: 1

6. Write changes to disk

Command (m for help): w

Writes the partition table to disk and exits.

7. Quit without saving

Command (m for help): q

Exits without modifying the disk.

Important options

-f       # Use a script file
-l       # List partition tables for all devices
-u       # Display sectors in cylinders or sectors

Partition type codes

CodeFilesystem Type
83Linux
82Linux swap
7HPFS/NTFS/exFAT
bW95 FAT32
cW95 FAT32 (LBA)
a5FreeBSD

To list all type codes:

Command (m for help): L

Post partitioning

After creating partitions, always format them:

mkfs.ext4 /dev/sdX1
mkfs.vfat /dev/sdX2

Mount the partition:

mount /dev/sdX1 /mnt/mydisk

Helpful tips

  • Use partprobe or reboot after changing partition table:
    sudo partprobe /dev/sdX
    
  • Use lsblk or blkid to view and identify new partitions:
    lsblk
    blkid