How To Use Fdisk Command To Manage Linux Partitions

If you're a Linux user, managing your local hard drives is an important task that requires careful attention. Your hard drives contain sensitive information, as well as the operating system and applications that you use on a daily basis. That's why it's crucial to have a reliable tool for managing your partitions.

In this comprehensive tutorial, we'll guide you through the process of using the Fdisk command to manage your Linux partitions.

What is Fdisk?

Fdisk is a powerful text-based utility that allows you to view and manage your hard drive partitions with ease.

We'll cover everything you need to know about Fdisk, from its basic functions to its more advanced features. We'll also provide tips and tricks for optimizing your partition management process and troubleshooting any issues that may arise. .

If you're using Ubuntu, Linux Mint, or any other distributions derived from Ubuntu, it's important to note that the Fdisk and Mkfs commands must be preceded by "sudo" to be executed as the root user. In distributions that don't use "sudo," you'll need to use the "su" command to get a Root Shell before executing each command without "sudo." .

Whether you're new to Linux or an experienced user, mastering the Fdisk command is essential for efficient and effective partition management. So why wait? Follow our step-by-step guide today and start exploring the world of Linux partition management!.

For this tutorial, we will use Ubuntu 22.10.

List Current Linux Partitions

To list the partitions of the current hard disk we will execute the following line:

sudo fdisk -l

List Current Linux Partitions

There we found detailed information such as

  • Partition route
  • Sectors
  • ID and type

It is possible to add the name of a disk device to show only the partitions associated with it. For example, we can use the following command to show only the partitions on the first disk device:

sudo fdisk -l /dev/sda

List Current Linux Partitions

How To Get & Use The Command Mode

To work with partitions of a hard disk, it will be necessary to enter the command mode.

For this it will be necessary to use the device name of a disk from the fdisk -l command.

The following command allows us to enter the command mode for the first disk device:

sudo fdisk /dev/sda

We can see that we access the command mode of fdisk:

How To Get & Use The Command Mode

Note: It is not possible to edit partitions while they are in use. If we want to modify partitions of the system, we must first boot from a live CD.

In command mode, we will use single-letter commands to specify the actions we want to perform.

Enter the letter m and press Enter to see a list of the commands available for use:

How To Get & Use The Command Mode

Display the partition table

We can use the p parameter to print the current partition table in the terminal from the command mode:

Display the partition table

How To Create a Linux Partition

Creating a Linux partition is a common and practical task for managing hard drives. To accomplish this, we can use the "n" command to create a new partition. We have the option to create a logical or primary partition using the "l" or "p" command respectively. Keep in mind that a disk can only have four primary partitions.

Next, we will need to specify the sector on the disk where we want the partition to start. If we accept the default sector, it will be the first free sector on the drive. We then specify the last sector of the partition on the disk. If we want to use all available space after the first sector, we can simply press Enter. Alternatively, we can specify a specific size using units such as "G" for gigabytes or "M" for megabytes. If we don't specify a unit, fdisk will use sectors as the default unit. For example, "+10000" would result in the end of the partition being 10000 sectors after its start.

How To Create a Linux Partition

We will use the command d to eliminate a partition, at the moment of its execution, we will be asked for the number of the partition that we want to remove, which can be obtained from the p command.

For example, if I wanted to delete partition 1 in / dev / sdb, we enter the respective number:

How To Create a Linux Partition

If we execute the p command again, we will see that the selected partition is a “Linux” partition, now we see it in the Type column:

How To Create a Linux Partition

If we want to change its type, we can use the command t and specify the partition number. We will be asked for the hexadecimal code of the type, if we do not know, we can write L to see a list of hexadecimal codes:

There we must enter the desired number, for example, if we want the type of partition to be a swap, enter the number 82 and press Enter:

How To Create a Linux Partition

This will not format the partition with the selected file system. We must do this later by executing mkfs.

Write changes

  • We will use w to write the changes that have been made to the disk
  • We will use q if you want to exit fdisk without saving the changes

How to Format a Linux Partition

You need to format new partitions with a file system before you can use them. This can be done with the appropriate mkfs command.

How to Format a Linux Partition


The syntax to use is:

  • sudo mkfs.(Type) Path

The options for type are

  • bfs
  • cramfs
  • ext2
  • ext3
  • ext4
  • ext4dev
  • minix
  • msdos
  • NTFS
  • vat

In this case, we will format the partition /dev/sdb with ext4:

sudo mkfs.ext4 /dev/sdb

If we want to use that partition as swap, we must execute the following line:

sudo mkswap /dev/sdb

We see how fdisk becomes an ally when it comes to managing disk partitions in Linux.

It might be interesting

Intel's Arc A770M and A550M mobile graphics cards made their first 3DMark test

First results of performance tests of unreleased Intel Arc A770M and Arc A550M mobile graphics gas pedals appeared in 3DMark synthetic test database.

Google unveiled a test version of Chrome OS Flex

Google has unveiled a test version of Chrome OS Flex. You can download Chrome OS Flex directly from a flash drive or install it on your PC's hard drive.

Forza 3 Has Pushed Xbox Further Ahead Than Ever Before

A 360 Hz physics engine is being done under the hood of a racer car from Forza 3. This is going to push Xbox 360 into developers with a group product that manages their games in a new way.

How To Sign Files in Linux with GPG

In this tutorial we show you how to sign and encrypt files with GPG on Linux systems digitally.