fdisk
The fdisk (format disk) command is a dialog-driven program for creation and manipulation of partition tables. It understands DOS-type partition tables and BSD or SUN-type disk labels.
Note: fdisk is traditionally used for MBR (Master Boot Record) partition tables. For modern GPT (GUID Partition Table) disks, gdisk or parted is often preferred, though newer versions of fdisk also support GPT.
Warning: Modifying partition tables can destroy data. Double-check the device name before proceeding.
Basic Syntax
fdisk [OPTIONS] DEVICE
(e.g., sudo fdisk /dev/sdb)
Common Flags/Options
| Option | Description |
|---|---|
-l, --list | List partition tables for all or specified devices and exit. |
-u | Change display units (usually to sectors). |
Interactive Commands (Inside fdisk)
Once you run sudo fdisk /dev/sdX, you enter an interactive prompt. Common commands include:
| Command | Action |
|---|---|
m | Print the help menu. |
p | Print the current partition table. |
n | Create a new partition. |
d | Delete a partition. |
t | Change a partition type (e.g., from Linux to Swap). |
w | Write the changes to disk and exit (This applies the changes!). |
q | Quit without saving changes. |
Real-world Examples
1. List all partition tables
This is a quick way to view the partition structure of all attached disks, similar to lsblk but with different formatting.
sudo fdisk -l
2. Create a new partition
Assume you added a new blank disk /dev/sdb.
sudo fdisk /dev/sdb
Interactive flow:
- Type
nfor a new partition. - Choose
pfor primary. - Accept the default partition number (1).
- Accept the default first sector.
- Enter
+20Gfor the last sector (creates a 20GB partition). - Type
pto verify it looks correct. - Type
wto write changes to disk.
3. Change partition type to Swap
If you want to use /dev/sdb2 as swap space:
- Run
sudo fdisk /dev/sdb. - Type
t. - Select partition
2. - Type
82(the hex code for Linux swap). - Type
wto save.