fdisk — Partition Disks with fdisk and parted

Partition disks on Linux: create and edit partition tables with fdisk and parted, build filesystems and set up swap.

fdisk is the classic tool for editing partition tables on Linux interactively – modern versions handle both MBR and GPT. parted complements it with native GPT support and the ability to grow or shrink partitions after the fact. Both reach deep into your storage devices: pick the wrong device or write too hastily, and your data becomes unreachable. So always confirm which device you are working on with lsblk or fdisk -l, and back up the partition table beforehand with sfdisk -d.

fdisk — View Partitions

fdisk -l — List all disks and their partition tables.

sudo fdisk -l

fdisk -l <device> — List partitions on a specific disk.

sudo fdisk -l /dev/sda

fdisk -x <device> — Show extended/expert partition info.

sudo fdisk -x /dev/sda

fdisk — Partition Management

fdisk <device> — Start interactive partitioning on a device.

sudo fdisk /dev/sdb

n — Create a new partition (inside fdisk).

Command: n primary partition number first/last sector

d — Delete a partition (inside fdisk).

Command: d partition number

t — Change partition type (inside fdisk).

Command: t partition number type code (83=Linux, 82=swap)

p — Print the partition table (inside fdisk).

Command: p

w — Write changes and exit (inside fdisk).

Command: w

q — Quit without saving (inside fdisk).

Command: q

parted — View & Create

parted -l — List all disks and partitions.

sudo parted -l

parted <device> print — Show partition table of a device.

sudo parted /dev/sda print

parted <device> mklabel <type> — Create a new partition table (gpt or msdos).

sudo parted /dev/sdb mklabel gpt

parted <device> mkpart <name> <fs> <start> <end> — Create a partition.

sudo parted /dev/sdb mkpart primary ext4 1MiB 100%

parted <device> rm <number> — Remove a partition by number.

sudo parted /dev/sdb rm 2

parted — Resize & Modify

parted <device> resizepart <number> <end> — Resize a partition (grow or shrink).

sudo parted /dev/sda resizepart 2 100%

parted <device> name <number> '<name>' — Set a partition name (GPT only).

sudo parted /dev/sda name 1 'boot'

parted <device> set <number> boot on — Set the boot flag on a partition.

sudo parted /dev/sda set 1 boot on

parted <device> set <number> lvm on — Mark a partition for LVM.

sudo parted /dev/sdb set 1 lvm on

Filesystem Creation

mkfs.ext4 <partition> — Create an ext4 filesystem.

sudo mkfs.ext4 /dev/sdb1

mkfs.xfs <partition> — Create an XFS filesystem.

sudo mkfs.xfs /dev/sdb1

mkfs.vfat -F 32 <partition> — Create a FAT32 filesystem.

sudo mkfs.vfat -F 32 /dev/sdb1

mkswap <partition> — Set up a swap partition.

sudo mkswap /dev/sdb2

swapon <partition> — Enable a swap partition.

sudo swapon /dev/sdb2

Conclusion

fdisk and parted are powerful but unforgiving of careless mistakes: target the wrong device and you may wipe an entire drive. Internalise a few safety rules. In fdisk, changes are only written to disk with w (write) – until then q (quit without saving) is your safety net, and after w there is no undo. Verify the device name with lsblk and fdisk -l before every action, and back up the existing partition table with sfdisk -d /dev/sdX > table.sav before you change anything. For drives larger than 2 TB and modern UEFI systems, use GPT instead of MBR – fdisk can do this nowadays, though many people still reach for gdisk or parted. And remember: a freshly created partition is still empty – only mkfs creates the filesystem, and doing so erases any data already on it.

Further Reading

  • lsblk – show disks and partitions as a tree, ideal for a device check before any fdisk action
  • mount – mount filesystems once you have partitioned and formatted a disk
  • dd – back up or clone disks and partition tables block by block