Fdisk Command in Linux – Options + Examples

Last updated: August 28, 2022

As a Linux geek, you are not satisfied with one operating system. You need divisions on your hard disk called partitions to install multiple distributions. Partitions behave as separate hard disks letting you use each sector individually.

Fdisk command enables you to create and manage partition tables. In this tutorial, we will learn about fdisk commands in Linux and perform operations on the hard disk.

What is fdisk command Linux

Fdisk or Fixed disk is a command line application used to manage the disk partitions in Linux. It is the most commonly used command mode application. It presents a menu in front of the user through which operations are executed on that hard disk partition.

For example: In Linux partitions, we need a table where there is a root file system of file system type ext4 and a swap partition which is optional. In Windows, we need an NTFS file system. Fdisk is capable of identifying and creating/changing the partition table according to the user's needs.

With the fdisk command, you can manage disk partitions and perform operations like format disk, create logical partitions or primary partitions, completely delete a partition, apply flags like boot, swap, hidden, etc. on a specific disk partition, etc.

fdisk operations
fdisk operations

The alphabet symbols you see on the left in the image above represent an operation that the user has to pass in the command mode of the fdisk menu. We will see the usage in the next section.

Syntax

fdisk [options] [partition name]

Installation of fdisk in Linux

fdisk is already packed with Linux-based operating systems as it is a standard package.

Check if fdisk exists

which fdisk

Which should return the path of the installed directory.

If the output is empty or you have uninstalled the package you can install it with the following command for your distribution.

For Debian (Ubuntu, Kali, Deepin, etc)

sudo apt install fdisk

For Arch Linux(Manjaro, Garuda, EndeavourOS, etc)

sudo pacman -S fdisk

For Fedora

sudo dnf install fdisk

For Redhat / CentOS Stream

sudo yum install fdisk

Now, launch a new terminal instance type the following command to get started

fdisk --help
fdisk help - Ubuntu

Please note that fdisk is used to manage partitions and requires root privileges (sudo) for its operations. You can either enter the root shell with the following command.

sudo su

Or just use sudo every time operating on the hard disk or partitions.

For example

sudo fdisk /dev/sda

How to use fdisk in Linux

fdisk is a command mode application and can be used with the help of a Linux terminal.

Display All Disk Partitions

Before performing any operations you will need to know what partition table entries are present in your current hard disk and display other things like external hard disk partitions.

You can display all disk partitions using the following command:

sudo fdisk -l
fdisk list all partitions

-l option is used to list partition table(s).

If you want to display only a specific partition table you can pass that device name with the fdisk command.

sudo fdisk -l /dev/sda
fdisk show specific partitions

If I want to display more than one partition table I will add partition names with the command.

sudo fdisk -l /dev/sda /dev/sdb
fdisk show multiple partitions

Create a new partition

After listing all the partitions note the disk or device name you want to create new partitions on. To create a new partition we will just have to pass the partition or disk/device name without any options with the fdisk command. It will present you with a menu like the one below.

Step 1: Choose the disk

Any disk of your choice:

sudo fdisk /dev/sda
choose disk to partition

You can type command m for help to explore more operations we can perform.

Step 2: Create a new partition

We are creating a new partition table, therefore we type the n command -

fdisk create new partition

Step 3: Choose partition type

Next, it is asking for the partition types - Primary partition or Logical partition for the partition you are creating. As I am planning to install another Linux distribution I will create a primary partition. If I want to create a logical partition then I will select extended partition.

The primary partition is generally a bootable partition and the extended partition is one or more collections of logical partitions.

choose fdisk partition type

Step 4: Give partition Number and Size

The next prompt is for partition number - It is the number assigned to the partition name. The range (1-4) may vary from system to system. For example - The partition number for /dev/sda will be /dev/sda1, /dev/sda2, etc. And for /dev/sdb it will be /dev/sdb1, /dev/sdb2, etc.

First Sector - It is the best practice to go with the default value.

Last Sector - Here you have to give the size of the partition you wish to create. I have created a 5 GB partition, by writing +5GB in the command mode.

provide new partition size

Step 5: Write changes to the disk

Finally, we hit the w command to write the changes to the disk.

write changes to disk

You should have a look at all the commands I have passed to create a new partition in the above image.

Step 6: Check changes

Let's verify whether we have created the partition or not by listing the partition table on that disk.

sudo fdisk -l /dev/sdb
verify new partition

Format a partition

If you want to delete all data in a partition and use the space for other purposes you can easily do that with the mkfs command. Unfortunately, fdisk doesn't have this feature. Please note that to format partitions in Linux it has to be unmounted first.

Syntax
sudo mkfs -t {partition type} {partition name}

To format an ext partition

sudo mkfs -t ext4 /dev/sdb1

To format a fat32 partition

sudo mkfs -t vfat /dev/sdb1

To format a gpt partition

sudo mkfs -t gpt /dev/sdb1

Mounting a partition

To use a partition, you need to mount it first. I have a /mnt folder in the root directory where I mount partitions.

sudo mount -t auto /dev/sdb1 /mnt

Unmounting a filesystem

umount /dev/sdb1

Delete a partition

To completely delete a partition using fdisk first, let's just list all the partitions.

sudo fdisk -l /dev/sdb

Then, enter command mode using fdisk.

sudo fdisk /dev/sdb

Then, pass the d command for deleting a partition. And specify the partition number as below.

fdisk delete partition

To finally delete the partition pass the w command to write the changes to the disk.

To delete all partitions repeat the above steps of specifying the partition number and finally pass the w command to write changes to the disk.

Check Partition Size

To check the partition size you can either list the partitions.

sudo fdisk -l /dev/sdb

To see the partition size.

Or, to display the size in blocks.

sudo fdisk -s /dev/sdb1
fdisk check partition size

Conclusion

In this tutorial, we learned how to use fdisk commands for managing hard disk partitions. Fdisk is a great command line tool as it presents a menu in front of the user making it easy to use, for even beginners. Create more and more partitions and play with your hard disk partitions.

SHARE

Comments

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

Leave a Reply

Leave a Comment