In this article, you will learn about the fdisk command in Linux.
When you use Linux or Unix-like operating system, you feel entirely different. There are a lot of things; you can do easily using the command-line utility. Many Linux operating systems are graphical user interface based, but you can do the intricate task very nicely using CLI. But this is not our today topic to discuss Linux or Unix history; here we are going to discuss fdisk command. So let’s begin.
fdisk stands for format disk is a command-line utility that we use to view and manage disk partitions in Linux or Unix-like operating systems. Here, manage means create, resize, change, copy, move, or delete the partitions of any hard disk, flash drive, solid-state drive, or any other computer data storage medium. We can create a maximum of four primary partitions and many logical partitions that depend on storage device space.
But, here I’m talking only about MBR based not GPT partition because theoretically, you can create an unlimited primary partition using GPT. So, lets’ start fdisk command in Linux.
How to get help for the fdisk command
I always suggest reading the manual page and help. It will give you an in-depth introduction to the command.
So, to get the manual page type the below command,
man fdisk
But, to get the only command options, you can type the below command.
fdisk --help
Syntax of the fdisk command
Now you know how to get help about the fdisk command. So, let’s talk about fdisk syntax. The syntax of the fdisk command is as below,
Syntax 1: fdisk [options] device
Syntax 2: fdisk -l [device…]
Precondition
- You must log-in with a root user or a user with sudo privilege.
Listing all the partitions using fdisk command
You can list all the available partitions on the disk. And this list comes along with many good descriptions. So, the command will look like the below and you can see the output in the below picture as well.
sudo fdisk -l

View the particular partition on a disk using fdisk command
As you can see in the above picture, there are two partitions one is /dev/sda1 and other /dev/sda2. So if you want to see the description for /dev/sda2 then type the below command format. You can change the device name or partition name as per your requirement.
sudo fdisk -l /dev/sda2
As a result, you can see in the below picture.

Entering in command mode
If you want to execute a command like a disk resizes or deleting a partition, then you have to go in command mode. So, here you need to specify the name the disk partition along with the command.
For example, I have a disk /dev/sda
and I want to perform some task on the partition /dev/sda2
then the command will look like the below way.
sudo fdisk /dev/sda2
Also, you can see the output in the below picture.

Note: I always suggest that you don’t edit any partition when they are in use. But, you can use any live CD to edit.
Now, To get all the options press m and hit the enter key. So, you will get the below options.

Command (m for help): m
Help:
DOS (MBR)
a toggle a bootable flag
b edit nested BSD disklabel
c toggle the dos compatibility flag
Generic
d delete a partition
F list free unpartitioned space
l list known partition types
n add a new partition
p print the partition table
t change a partition type
v verify the partition table
i print information about a partition
Misc
m print this menu
u change display/entry units
x extra functionality (experts only)
Script
I load disk layout from the sfdisk script file
O dump disk layout to the sfdisk script file
Save & Exit
w write table to disk and exit
q quit without saving the changes
Create a new label
g create a new empty GPT partition table
G creates a new empty SGI (IRIX) partition table
o create a new empty DOS partition table
s create a new empty Sun partition table
Creating a hard disk partition
I have a hard disk with name /dev/sdb
and I want to create a new primary partition. So first I will use the below command to go inside the hard disk.
sudo fdisk /dev/sdb
As I am creating a new partition, so I have to type n, i.e. for adding a new partition. And I can choose either primary partition then press p or extended partition then press e. So I want primary partition, so I typed p. After that, you can press enter key, and then you can specify the partition size, I am putting the capacity for the partition +512M
Note: You have to put plus symbol and then type the size here, I’m putting 512 MB so for MB it’s M and for GB you have to put G.
So, to save the changes, we have to press w and then hit enter. If you don’t want to save the changes then you can press q for quit without saving. So, I am pressing w to save the changes.
As a result, you can see in the below picture.

Formating the partition
If you want to use the above partition then we have to format it. So, there is mkfs command for that.
For instance, I’m going to format this partition /dev/sdb1 using ext4.
mkfs.ext4 /dev/sdb1

** You can check the partition using the command fdisk -l /dev/sdb1
,
As a result, you can see in the below picture.

Deleting a hard disk partition
If you want to delete a partition and making free space then you can use the below command.
For example, I’m going to delete the same partition we have created that is /dev/sdb
sudo fdisk /dev/sdb
After typing the above command hit the enter key, It will ask for command, type d (for delete) and hit the enter key, it will ask the partition number, enter the number. that’s it
As a result, you can look in the picture.

Conclusion
In this tutorial, you learned about the fdisk command in Linux. But, this command is a little hard but the benefits are excellent, so use this command. So, I hope, you understand, but if you have any questions, you can ask in the comment section.
Also, you can read about