Quick take: The fdisk command partitions disks. sudo fdisk -l lists all disks and partitions, and sudo fdisk /dev/sdX opens an interactive editor where n creates, d deletes, p prints, and w writes changes. Editing partitions can destroy data.
Introduction
The fdisk command creates and manages disk partitions through an interactive menu. It is a core tool when adding a new disk or repartitioning storage, but it is also dangerous: writing changes to the wrong disk destroys data. Always confirm the device first and back up anything important.
This guide covers listing partitions, the interactive commands to create and delete them, and writing changes safely.
Syntax
The basic syntax of the fdisk command is:
fdisk [OPTIONS] DEVICECommon Options and Parameters
The most useful options and parameters for the fdisk command:
| Option | Description |
|---|---|
| -l | List the partition tables of all disks. |
| DEVICE | Open the interactive partition editor for a disk. |
| p (interactive) | Print the current partition table. |
| n (interactive) | Create a new partition. |
| d (interactive) | Delete a partition. |
| t (interactive) | Change a partition's type. |
| w (interactive) | Write changes to disk and exit. |
| q (interactive) | Quit without saving changes. |
Practical Examples
Real fdisk commands you can run today:
# List all disks and partitions
sudo fdisk -l
# List a single disk
sudo fdisk -l /dev/sda
# Open the interactive editor
sudo fdisk /dev/sdb
# (inside) print, new, write, quit
p n w q
# Confirm the device first with lsblk
lsblkTips and Best Practices
- Confirm the device with
lsblkbefore editing. Partitioning the wrong disk destroys its data with no undo. - Nothing is changed until you press
w— useqto quit safely if you are unsure, andpoften to review. - fdisk traditionally handled MBR disks; for very large disks and GPT,
partedorgdiskis the better choice (modern fdisk supports GPT too).
Final Thoughts
fdisk is the interactive tool for partitioning disks, with -l to list and an editor where n, d, p, and w do the work. Its power is matched by its risk: verify the device with lsblk, review with p, and remember nothing is written until w. After partitioning, create a filesystem with mkfs and mount it.
FAQ: fdisk Command in Linux
How do I list disk partitions in Linux?+
Run sudo fdisk -l to show the partition tables of all disks, or sudo fdisk -l /dev/sda for one disk. lsblk gives a friendlier tree view.
How do I create a partition with fdisk?+
Open the disk with sudo fdisk /dev/sdX, press n to create a partition, accept or set the size, then press w to write the changes. Confirm the device first with lsblk.
How do I exit fdisk without saving?+
Press q to quit without writing any changes. fdisk only modifies the disk when you press w, so q is always a safe way out.
Is fdisk safe to use?+
fdisk is safe for listing (fdisk -l), but editing partitions can destroy data if you choose the wrong disk. Verify the device with lsblk, back up important data, and review with p before writing with w.
Should I use fdisk or parted?+
fdisk is fine for standard disks and is simple and interactive. parted handles very large disks and GPT partitioning with more flexibility and can also script operations, making it better for advanced needs.
Need help with Linux servers or infrastructure?
Work directly with Muhammad Irfan Aslam for Linux, Ubuntu, Docker, DevOps, cloud, CI/CD, or infrastructure support.
Hire Me for Support