Looking For Anything Specific?

How to Manage Disk Partitions in Linux With fdisk

Managing partitions is one of the most tedious operations on Linux. If you're new to the operating system, you might even find the process confusing at the outset.

But since disk partitioning is an essential step in OS installation and file organization, among other things, it's imperative to know how to manage partitions on your computer.

To this end, here's a guide on how to use fdisk to create, resize, and delete partitions on Linux.

What Is fdisk?

Fdisk is a text-based command-line utility for viewing and managing hard disk partitions on Linux. It supports various partition tables and lets you create, delete, and modify disk partitions, so you can utilize the disk space effectively to install operating systems, back up your data, and organize your files.

How to Install fdisk on Linux?

Fdisk is a standard partitioning utility, and therefore you'll find it pre-installed on most Linux distros out there. To find out if your system has fdisk, open the terminal and run:

which fdisk

If it returns a path, that means fdisk is present on your system. In case it doesn't, you need to install it manually.

On Debian-based systems:

sudo apt install fdisk

On Fedora:

sudo dnf install fdisk

On Arch-based distros:

sudo pacman -S fdisk

On CentOS:

sudo yum install fdisk

How to Use fdisk on Linux

Once you've installed fdisk, follow the sections below to learn how to use it for performing different disk management operations on your system.

Listing All Disk Partitions

First and foremost, let's list out the partitions on your system. We can do this using the -l argument with the command.

In the terminal window, type the following command and press Enter:

sudo fdisk -l

Output:

Related: Should You Partition Your Hard Drive? The Pros and Cons

Entering and Using Command Mode

After you've listed the available partitions on your system, you now need to enter command mode to be able to perform any operations on your disk.

For this, choose the device on which you want to carry out operations and run the following command:

sudo fdisk /dev/sda

Make sure to replace /dev/sda in the above command with the device name of your choice.

Once in command mode, you can now use letters to perform different operations. If you're using fdisk for the first time, type m and hit Enter to get a list of all the supported commands.

Viewing the Partition Table

One of the basic partition management actions you should perform before moving on to creating, formatting, or deleting partitions on your drive is to view the partition table.

Doing so gives you all the information about the logical disks or partitions on your hard disk, using which, you can then decide how you'd like to manage partitions on your disk.

To view the partition table in Linux, enter p and hit Enter. Keep in mind that this command will come in handy even in later stages—after deleting, creating, or formatting a partition—to find out the status of the partition table at the time.

Deleting a Partition

After assessing the partition table, if you wish to delete a partition to free up space or prepare your disk to extend another partition, you can use the d command to delete the partition.

To do this, in the terminal, enter d and press Enter. Fdisk will now ask you to pick a partition you want to delete. Scroll up to the output of the p command and find the number adjacent to the partition you want to delete.

Type the number and hit Enter.

For example, to delete the sda6 partition on your disk, enter p followed by 6.

Creating a Partition

Once you've freed up some space on your drive, creating a new partition is just a matter of following the steps below.

First, let's start by initiating the partition creation process. For this, type n and press Enter. Once you've done that, fdisk will ask you which kind of partition you want to create. Options for which include:

  • p: primary (1-4)
  • l: logical (5 or over)

Depending on your requirements, enter the appropriate letter corresponding to the partition type above and hit Enter.

Next, you need to specify the sector at which you want the partition to start. If you’d like to go with the default value, press Enter.

Similarly, you also have to specify the last sector on the disk. Hit Enter to allow the partition to go with the default setting, which is to use the remaining disk space. Alternatively, to use a specific value, prepend the number with a plus (+) sign and press Enter.

Related: How to Repair a Dead Hard Disk Drive to Recover Data

Formatting a Partition

Finally, after you've deleted partitions to free up disk space and created a new partition, you need to format the partition to use it.

The idea behind this is to remove unnecessary files on the partition or fix errors that may have corrupted the partition and rendered it inaccessible. But since the fdisk utility doesn't include the partition formatting functionality, we have to resort to another program. For this guide, we'll use mkfs.

Mkfs or Makes File Systems is essentially a command that lets you build file systems—format partitions to specific file systems—on Linux. It supports various file systems and has a different tool for each one of them. So depending on your requirement, you can construct your mkfs command accordingly.

For example, to format the sda6 partition on your disk in the ext4 format, you'd run:

sudo mkfs.ext4 /dev/sda6

Writing Partition Changes to the System

All the partition management operations we've performed so far have only been executed by fdisk up to this point: they've not been applied/saved to the file system.

So, hereon, you have two options. You can either write your changes to the disk to apply them or quit fdisk without saving the changes. The latter option can come in handy if you accidentally make a mistake with some partitioning operation in fdisk.

To write your changes to the disk, enter w and hit Enter. Alternatively, to exit fdisk and undo your operations, enter q.

Successfully Managing Linux Partitions With fdisk

With the help of this guide, you should be able to manage disk partitions in Linux easily and subsequently be able to get more out of the storage, be it for installing a new OS, dual-booting another OS, or improving file management.

If you're just getting started with Linux, though, cfdisk is an easier alternative to fdisk that you should consider checking out. It's essentially a graphical tool designed with a beginner-friendly interface that makes Linux partition management a bit easier.


Post a Comment

0 Comments