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 -lfdisk -l <device> — List partitions on a specific disk.
sudo fdisk -l /dev/sdafdisk -x <device> — Show extended/expert partition info.
sudo fdisk -x /dev/sdafdisk — Partition Management
fdisk <device> — Start interactive partitioning on a device.
sudo fdisk /dev/sdbn — Create a new partition (inside fdisk).
Command: n → primary → partition number → first/last sectord — Delete a partition (inside fdisk).
Command: d → partition numbert — Change partition type (inside fdisk).
Command: t → partition number → type code (83=Linux, 82=swap)p — Print the partition table (inside fdisk).
Command: pw — Write changes and exit (inside fdisk).
Command: wq — Quit without saving (inside fdisk).
Command: qparted — View & Create
parted -l — List all disks and partitions.
sudo parted -lparted <device> print — Show partition table of a device.
sudo parted /dev/sda printparted <device> mklabel <type> — Create a new partition table (gpt or msdos).
sudo parted /dev/sdb mklabel gptparted <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 2parted — 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 onparted <device> set <number> lvm on — Mark a partition for LVM.
sudo parted /dev/sdb set 1 lvm onFilesystem Creation
mkfs.ext4 <partition> — Create an ext4 filesystem.
sudo mkfs.ext4 /dev/sdb1mkfs.xfs <partition> — Create an XFS filesystem.
sudo mkfs.xfs /dev/sdb1mkfs.vfat -F 32 <partition> — Create a FAT32 filesystem.
sudo mkfs.vfat -F 32 /dev/sdb1mkswap <partition> — Set up a swap partition.
sudo mkswap /dev/sdb2swapon <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
- ArchWiki: Partitioning – thorough, distribution-agnostic reference covering MBR, GPT, fdisk and parted
- GNU Parted Manual – official parted manual with all commands and options