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.
The basic syntax of fdisk as follows:
fdisk [options] device
Here are some frequently used fdisk options and what they do:
--list: List all partitions on the specified device.
--new: Create a new partition.
--delete: Delete an existing partition.
--type: Change the partition type.
--units: Set the display units for sizes.
--cylinders: Use cylinder mode for partition display.
--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:
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.
To get a comprehensive overview of all the partitions, their sizes, types, and other details, run fdisk -l without specifying a device.
sudo fdisk -l
But if you want to list information only about the partitions on that particular device, then specific device name.
sudo fdisk -l /dev/sda
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.
sudo fdisk /dev/sdb
Inside interactive mode, use the following commands:
nto 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
pto 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
To save the changes and exit fdisk, press
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.