fdisk Command
The fdisk command is a powerful, menu-driven command-line utility for creating, deleting, and modifying disk partitions on Linux systems. It is commonly used for managing hard disk partitions, including creating new partitions, deleting existing ones, and changing partition types.
Syntax
Description
fdisk works with partition tables, primarily supporting MBR (Master Boot Record) and GPT (GUID Partition Table) formats. It provides an interactive interface where you can issue commands to manipulate the disk's partitioning scheme. Due to its direct interaction with disk structures, caution is advised when using fdisk.
Common uses include:
- Viewing existing disk partitions
- Creating new primary or logical partitions
- Deleting partitions
- Changing a partition's system ID
- Setting a partition as bootable
Common Options
| Option | Description |
|---|---|
-l |
List the partition tables for the specified devices |
-s <partition> |
Print the size of a partition in blocks |
-v |
Display version information |
-u |
When listing partitions, give sizes in sectors instead of cylinders |
Interactive Commands (within fdisk)
| Command | Description |
|---|---|
m |
Print this menu (help) |
p |
Print the partition table |
n |
Add a new partition |
d |
Delete a partition |
t |
Change a partition's system ID |
w |
Write table to disk and exit |
q |
Quit without saving changes |
Examples
List all disk partitions
Lists the partition tables for all available disk devices (e.g., /dev/sda, /dev/sdb).
Enter interactive mode for a specific disk
Starts the interactive fdisk utility for the /dev/sda disk. You will then use commands like p, n, d, w, q.
Create a new primary partition (interactive steps)
# Command (m for help): n # Select (p)rimary or (e)xtended: p # Partition number (1-4): 1 # First sector (2048-..., default 2048): [Press Enter] # Last sector or +size or +sizeM or +sizeK: +10G # Command (m for help): w
Example steps to create a new 10GB primary partition on /dev/sdb.
Delete a partition (interactive steps)
# Command (m for help): d # Partition number (1-4): 1 # Command (m for help): w
Example steps to delete partition 1 on /dev/sdb.
⚠️ Important Notes
- Using
fdiskincorrectly can lead to data loss. Always back up important data before making changes to disk partitions. - Changes made in interactive mode are not written to disk until you use the
wcommand. - You typically need root privileges (
sudo) to runfdisk. - For GPT partition tables,
gdiskis often preferred overfdisk.