>_cmd.script

fdisk

View or modify a disk's partition table

Disk

By CMD Script Team · 5 min read · Last updated

SYNTAX
fdisk [OPTIONS] DEVICE

Options

Command options and flags
FlagDescription
-lList the partition table(s) for the given device(s), or all detected devices if none given, then exit
-uShow partition sizes in sectors instead of cylinders in the interactive display
-s PARTITIONPrint the size of a partition in blocks, then exit
(interactive) nInside the interactive prompt: create a new partition
(interactive) dInside the interactive prompt: delete an existing partition
(interactive) pInside the interactive prompt: print the current in-memory partition table
(interactive) wInside the interactive prompt: write changes to disk and exit
(interactive) qInside the interactive prompt: quit without saving changes

Distribution compatibility

  • Ubuntu
  • Debian
  • Fedora
  • Arch

What it does

fdisk views and edits the partition table on a block storage device — the structure that defines how a disk is divided into partitions, their sizes, and their types. Running it with -l is a safe, read-only way to inspect existing partitions. Running it against a device with no flags drops you into an interactive prompt where you can create, delete, resize (via delete-and-recreate), and change the type of partitions before writing those changes to disk.

Beginner examples

  • sudo fdisk -l — list partition tables for every detected disk
  • sudo fdisk -l /dev/sda — list the partition table for one specific disk
  • sudo fdisk /dev/sdb — open /dev/sdb in the interactive partition editor
  • Inside the interactive prompt: p — print the current (in-memory) partition table
  • Inside the interactive prompt: m — show the help menu of available commands
sudo fdisk -l /dev/sda

Advanced examples

  • Create a new partition interactively: run sudo fdisk /dev/sdb, then type n, follow the prompts for partition type/number/size, then w to commit.
  • Delete a partition: inside the interactive session, type d, select the partition number, then w to commit — or q to back out without saving.
  • Change a partition's type ID (e.g. to Linux LVM or swap): type t inside the session, select the partition, and enter the new type code.
  • Check a specific partition's size in blocks without entering interactive mode: sudo fdisk -s /dev/sda1.
  • Inspect sector-level layout (useful for alignment checks): sudo fdisk -u -l /dev/sda.
sudo fdisk /dev/sdb
# then: n, p, 1, <accept defaults>, w

Common mistakes

  • Running fdisk against the wrong device (e.g. /dev/sda, the system disk, instead of /dev/sdb, an attached secondary disk) and writing changes — this can destroy the operating system's partition table.
  • Forgetting that changes are only staged in memory until w is pressed, then being surprised that a previous "test" session actually had no effect (because q was used) — or the opposite, forgetting to press q and accidentally committing an experiment.
  • Assuming fdisk formats the partition too — it only edits the partition table; you still need mkfs (e.g. mkfs.ext4) to create a filesystem on the new partition before it's usable.
  • Editing a partition table on a disk that has partitions currently mounted and in use, which can lead to a kernel view of the disk that's out of sync until a reboot or partprobe.

Tips

  • Always run fdisk -l first to confirm you have the right device name before opening it interactively.
  • Use lsblk or df -h alongside fdisk -l to cross-check which device holds which mounted filesystem before making changes.
  • After creating a partition, remember it still needs a filesystem (mkfs.ext4, mkfs.xfs, etc.) before it can be mounted and used.
  • If the kernel doesn't immediately see a partition table change, run partprobe or reboot to force it to re-read the table.

Best practices

  • Never run fdisk's interactive write (w) command on a production disk without a current backup — partition table mistakes can make all data on the disk unreadable.
  • Double- and triple-check the device path (/dev/sdX) before committing changes; typos between similarly named devices are the single most common cause of accidental data loss with fdisk.
  • Prefer fdisk -l (read-only) for routine inspection, and reserve interactive editing for deliberate, planned partitioning work.
  • For GPT disks or advanced resizing needs, consider parted or gdisk, which handle GPT semantics more directly than traditional fdisk.

Try it yourself

A simulated shell with a sample home directory — experiment freely, nothing leaves your browser. Type help to list supported commands.

Real-world use cases

  • Inspecting the partition layout of a newly attached disk before deciding how to partition and format it.
  • Creating a new partition on a freshly added virtual disk in a cloud VM, then formatting and mounting it for extra storage.
  • Diagnosing a boot issue by checking whether the expected boot/EFI partition still exists and has the correct type.
  • Auditing partition types across disks before a migration to confirm nothing unexpected (e.g. a leftover swap partition) is present.

Common interview questions

  • What's the difference between fdisk -l and running fdisk interactively? -l is a read-only listing of the partition table that exits immediately; running fdisk on a device with no other flags opens an interactive session where you can create, delete, or modify partitions before writing changes.
  • Does fdisk format partitions? No — fdisk only manages the partition table (the boundaries and types of partitions). You still need a tool like mkfs to put a filesystem on a partition before it can store files.
  • Why is fdisk considered risky? Because writing an incorrect partition table (via the w command) can overwrite the layout of an entire disk, potentially making existing data inaccessible, especially if you targeted the wrong device.
  • What does macOS use instead of fdisk? macOS uses diskutil for partition and volume management rather than the Linux fdisk utility.

Frequently Asked Questions

Is fdisk available on macOS?

No, not as the same tool. macOS uses diskutil (and the lower-level, less commonly used gpt) for partition management instead of the Linux fdisk utility.

Does fdisk change anything before you tell it to?

No. fdisk's interactive session builds up changes in memory only; nothing is written to the actual partition table until you press w. Pressing q exits without saving any changes.

Is fdisk safe to run?

Listing partitions with fdisk -l is completely safe and read-only. The interactive mode is where the risk lies: creating, deleting, or resizing partitions and then writing (w) can destroy all data on the affected partitions, especially if you pick the wrong device. Always double-check the device name before writing.

What's the difference between fdisk and parted?

fdisk is simpler and traditionally limited to MBR partition tables, though modern versions support GPT too. parted has more advanced GPT support and can resize partitions directly, while fdisk generally requires delete-and-recreate for resizing.

Cheat sheet

Download a quick-reference cheat sheet for fdisk.