LinuxCommandLibrary

fdisk

Manage disk partitions

TLDR

List partitions

$ sudo fdisk [[-l|--list]]
copy

Start the partition manipulator
$ sudo fdisk [/dev/sdX]
copy

Once partitioning a disk, create a partition
$ <n>
copy

Once partitioning a disk, select a partition to delete
$ <d>
copy

Once partitioning a disk, view the partition table
$ <p>
copy

Once partitioning a disk, write the changes made
$ <w>
copy

Once partitioning a disk, discard the changes made
$ <q>
copy

Once partitioning a disk, open a help menu
$ <m>
copy

SYNOPSIS

fdisk [options] device

PARAMETERS

device
    The block device to operate on (e.g., /dev/sda, /dev/nvme0n1). This argument is required for interactive mode or most non-listing operations.

-l
    List partition tables for the specified devices or all available devices if no device is given. This option forces non-interactive mode.

-s partition
    Print the size of the specified partition in 512-byte sectors.

-b sectorsize
    Specify the sector size of the disk. Defaults to 512 bytes.

-c
    Disable DOS compatibility mode. This can be useful for non-DOS partitions.

-u
    When listing partitions, give sizes in sectors instead of cylinders (useful for precise measurements).

-w
    Write the partition table to disk and exit. This is a non-interactive way to save changes after modifying the partition table (e.g., using `echo 'n w' | fdisk -w /dev/sdb`).

-N partnum
    Change the partition number of an existing partition.

-h
    Print a help message and exit.

-V
    Print version information and exit.

DESCRIPTION

fdisk is a powerful, menu-driven command-line utility used for disk partition manipulation on Linux and other Unix-like systems. It allows users to create, delete, resize, and modify partitions on hard drives, SSDs, and other storage devices.

While primarily known for its robust handling of MBR (Master Boot Record) partition tables, it also has basic support for GPT (GUID Partition Table), though specialized tools like gdisk or parted are often preferred for complex GPT operations.

fdisk interacts directly with the kernel's block device interface, making it an essential tool for preparing storage devices for file systems and operating system installations. Its default interactive mode presents a menu of options, guiding users through partition management tasks, while non-interactive options allow for scripting.

CAVEATS

Data Loss Risk: fdisk is extremely powerful. Incorrect usage can lead to permanent data loss. Always back up critical data before making changes.
Root Privileges: Requires root privileges to run.
MBR Focus: While it has some basic GPT support, fdisk is traditionally optimized for MBR partition tables. For GPT-formatted disks, gdisk or parted are generally recommended due to their native handling of GPT features.
Changes Not Immediate: In interactive mode, changes are only written to the disk's partition table when you explicitly use the 'w' command (write). Until then, changes are only in memory.
In-use Partitions: Modifying an in-use (mounted) partition can cause system instability or data corruption. It's best to unmount partitions or work from a live boot environment when performing critical changes.

INTERACTIVE MODE

When invoked without the -l option (e.g., fdisk /dev/sda), fdisk enters an interactive, menu-driven mode. Users can type single-letter commands to manipulate the partition table. Common interactive commands include:
n: Add a new partition
d: Delete a partition
p: Print the partition table
t: Change a partition's system ID
L: List known partition types
w: Write table to disk and exit (saves changes permanently!)
q: Quit without saving changes

Changes are only written to disk upon explicit confirmation with the w command, providing a crucial safeguard against accidental modifications.

PARTITION TABLE TYPES

fdisk primarily works with MBR (Master Boot Record) partition tables, which are commonly found on older systems or smaller drives. MBR supports up to four primary partitions or three primary and one extended partition, with the extended partition potentially containing multiple logical partitions. While fdisk has basic capabilities to read and manipulate GPT (GUID Partition Table) disks, for comprehensive and robust GPT management, tools like gdisk or parted are generally recommended as they natively support GPT's advanced features, such as larger disk sizes and a virtually unlimited number of partitions.

HISTORY

fdisk has been a fundamental utility in Unix-like systems for decades, tracing its origins back to the early days of disk management. It is part of the util-linux project, which bundles various essential system utilities for Linux. Its stable and mature codebase has made it a default choice for basic partition manipulation. While newer tools like parted and gdisk offer more modern features, especially for GPT, fdisk remains widely used due to its simplicity, efficiency, and robust handling of MBR partition tables.

SEE ALSO

parted(8), gdisk(8), sfdisk(8), mkfs(8), mount(8), lsblk(8)

Copied to clipboard