Linux fdisk Command: Inspect and Edit Disk Partitions

fdisk is a tool for viewing or editing a disk's partition table. It is useful for checking partition sizes and starting positions, but writing changes to the wrong disk can result in data loss.

What is the fdisk command?

fdisk -l is a read-only list view. In interactive editing, you create changes with several commands and must enter w to write them to the disk, while q exits without saving. It can handle GPT and DOS/MBR partition tables, but actual usable features depend on the util-linux version.

Basic syntax

fdisk [option] [disk device]

Installed implementations and options may vary by distribution. Use man fdisk to check the documentation on your current system.

Examples

Viewing the disk and partition list

Check the partition tables of all devices in read-only mode.

sudo fdisk -l

Viewing only the target disk

Insert the actual disk name identified by lsblk to narrow the scope.

sudo fdisk -l /dev/sdb

Device comparison before change

Check the model, capacity, and mount relationships again.

lsblk -o NAME,SIZE,MODEL,TYPE,MOUNTPOINTS

Distinguishing Between Viewing and Changing Partitions

fdisk -l only views the partition table and does not make any changes. Replace the device path in the following commands according to your actual system, but do not confuse the path of the entire disk with that of a partition.

sudo fdisk -l /dev/sdb

Part of the sample output (size and partition names vary depending on the environment):

Disk /dev/sdb: 20 GiB
Device      Start      End  Sectors Size Type
/dev/sdb1    2048 41943039 41940992  20G Linux filesystem

In interactive fdisk /dev/sdb, you can view the current table with p and exit without saving with q. w writes any changes to the disk, which can make recovery difficult. Before writing, check the device size again with lsblk and prepare a backup of the partition table and data.

Main Options and Format

Options/Format Description
-l List the partition table and exit.
-x Display extended partition information and additional fields.
-o column_list Select columns for list output.
-t type Specify the partition table type.
w / q In interactive mode, write changes and exit / exit without writing changes.

Precautions when using

Before editing partitions, create a recoverable backup and do not confuse the whole disk device with the partition device. Changing the partition boundaries of a file system in use is particularly risky. After the change, the kernel may not be able to immediately reread the new table, and a reboot may be required. Never use the example /dev/sdb for editing without verifying the actual target.

Frequently Asked Questions

If you press q in fdisk, are the changes saved?

No. In interactive mode, q exits without writing to the disk, while w writes the changed partition table.

Official Documentation

You can check the exact behavior of the options and implementation differences in the official fdisk documentation.

More in This Category
Linux mkdir Command: Create Directories and Parent Paths

Linux mkdir Command: Create Directories and Parent Paths

Learn how to create directories with Linux mkdir, build missing parent paths, set permissions, understand umask behavior, and verify results in scripts.

Linux nice Command: Set a New Process's Scheduling Priority

Linux nice Command: Set a New Process's Scheduling Priority

Learn how to set a New Process's Scheduling Priority with the Linux nice command, including practical examples, key options, and important precautions.

Linux chgrp Command: Change a File's Group

Linux chgrp Command: Change a File's Group

Learn how to change a File's Group with the Linux chgrp command, including practical examples, key options, and important precautions.

Linux tac Command: Print Lines in Reverse Order

Linux tac Command: Print Lines in Reverse Order

Learn how Linux tac prints records from last to first, handles multiple files and custom separators, and helps inspect recent log entries in reverse order.

Linux whatis Command: Show One-Line Command Descriptions

Linux whatis Command: Show One-Line Command Descriptions

Learn how to use the Linux whatis command to display concise descriptions of commands and manual pages, with essential options, practical examples, output interpretation, and common troubleshooting tips.

Linux dmesg Command: Read Kernel and Boot Messages

Linux dmesg Command: Read Kernel and Boot Messages

Learn how to read Kernel and Boot Messages with the Linux dmesg command, including practical examples, key options, and important precautions.

Linux fg Command: Bring a Shell Job to the Foreground

Linux fg Command: Bring a Shell Job to the Foreground

Learn how to bring a Shell Job to the Foreground with the Linux fg command, including practical examples, key options, and important precautions.

Linux Tutorial / Find Files and Commands with find, locate, which, and whereis

Linux Tutorial / Find Files and Commands with find, locate, which, and whereis

Compare find and locate for file searches, then use which, whereis, and Bash type to distinguish executable paths from shell command resolution.

Linux umount Command: Unmount a Filesystem Safely

Linux umount Command: Unmount a Filesystem Safely

Learn how to unmount a Filesystem Safely with the Linux umount command, including practical examples, key options, and important precautions.

Linux Tutorial / Understand Users, Groups, and File Ownership

Linux Tutorial / Understand Users, Groups, and File Ownership

Understand UIDs, GIDs, primary and supplementary groups, and file ownership, then inspect them safely with id, groups, getent, ls, and stat.