fdisk command in Linux Explained [with Examples]

Written by: Subhash Chandra   |   Last updated: August 28, 2022

fdisk is a powerful command-line utility in Linux used for managing disk partitions on your system. With fdisk, you can create, delete, resize, and manage partitions on your hard drives or storage devices. It can be used in both menu-driven and command-line modes, allowing users to choose the mode that best suits their preferences and requirements.

fdisk utility primarily used for managing Master Boot Record (MBR) partition tables. Apart it supports GPT, Sun, SGI, and BSD partition tables as well.

Syntax

The basic syntax of fdisk as follows:

fdisk [options] device

Here are some frequently used fdisk options and what they do:

  • -l or --list: List all partitions on the specified device.
  • -n or --new: Create a new partition.
  • -d or --delete: Delete an existing partition.
  • -p or --print: Print detailed information about a partition.
  • -t or --type: Change the partition type.
  • -u or --units: Set the display units for sizes.
  • -c or --cylinders: Use cylinder mode for partition display.
  • -s or --sectors: Use sector mode for partition display.

fdisk utility already comes packed with all modern Linux Distros. In case not found you can easily install using the respective package manager of the Distro.

sudo apt install fdisk  ###On Debian based system
sudo pacman -S fdisk ###On Arch Linux based system
sudo dnf install fdisk ### On Redhat/Fedora

Using fdisk Command

To access the menu-driven mode, simply run fdisk without any options and specify the target device. For example:

fdisk /dev/sda

In this mode, you can easily perform actions like creating, deleting, and modifying partitions using simple menu options.

Whereas in command line mode you can execute specific commands and options directly from the command line. For example, to list partitions on /dev/sda type fdisk -l /dev/sda from the terminal.

List Partitions

To get a comprehensive overview of all the partitions, their sizes, types, and other details, run fdisk -l without specifying a device.

Command:

sudo fdisk -l
list all partitions using fdisk -l

But if you want to list information only about the partitions on that particular device, then specific device name.

Example:

sudo fdisk -l /dev/sda
List partitions on a specific device

Create a new partition

Inside the fdisk interactive mode use -n option to create a new partition.

Start with the target disk device name as an argument.

Example:

sudo fdisk /dev/sdb
fdisk create a new partition

Inside interactive mode, use the following commands:

  • Press n to create a new partition.
  • Choose whether you want to create a primary or extended partition.
  • Specify the partition number (e.g., 1 for the first partition).
  • Define the starting and ending sectors for the partition, or simply press Enter to use the default values to use the entire available space.

After defining the partition parameters, press w to write the changes to the disk

Now you have created a new partition to use you should format the partitions and mount them to the system.

Delete a partition

To delete a partition on /dev/sda

sudo fdisk /dev/sdb
  • Press p to list the existing partitions on the disk.
  • Identify the partition number of the partition you want to delete.

Once you have identified the partition you want to delete, use the d command followed by the partition number to delete it

fdisk delete a partition

To save the changes and exit fdisk, press w.

One use case where you need to delete a partition is when you need to resize the partition (this should only be done on the last partition on the disk).

Toggle boot flag

Inside the fdisk interactive mode, you can toggle the boot flag for a partition using the "a" key. When you press "a," will prompt you to enter the partition number for which you want to toggle the boot flag.

You can verify the changes by using the "p" command to print the partition table.

Remember this "a" command only works for the DOS/MBR (Master Boot Record) partition table.

When working with non-DOS/MBR partition tables like GPT, it's advisable to use tools such as parted or gdisk that are designed for those partition table types.

About The Author

Subhash Chandra

Subhash Chandra

Subhash Chandra, an Oracle Certified Database Administrator and professional writer, works as a Consulting User Assistance Developer at Oracle, bringing over 15 years of experience to the role. He enjoys sharing his technological passion through how-to articles, simplifying complex concepts in Linux, Windows, Mac OS, and various other platforms and technologies for a wide audience.

SHARE

Comments

Please add comments below to provide the author your ideas, appreciation and feedback.

Leave a Reply

Leave a Comment