Managing Hard Disk Partitions Using fdisk [Linux]

Even the simplest, single hard drive installation of Linux where the whole disk is used for the OS probably has multiple partitions on the disk. If you need to work with the partitions on a disk, Linux provides several different tools including fdisk.

fdisk is a menu based, interactive command line tool that allows you to view, create, modify and delete partitions on a disk. In Linux, all devices are named according to special files found in the /dev directory. A typical SATA hard disk is named /dev/sda. To see a list of hard disks on your system use the “lshw” command:

sudo lshw -class disk

The output shows the hard drives and optical drives attached to the system:

Using lshw to list hard drives

To non-interactively list the partition table on the first hard drive, use:

sudo fdisk -l /dev/sda

The output will look something like this:


This shows that the first partition /dev/sda1 is the biggest partition and is a Linux partition. Since it is the only Linux partition, we also know that it is the root partition (or the system partition). sda2 is an extended partition (which can be subdivided into multiple logical partitions) and sda5 is the first (and only) logical partition in the extended partition. sda5 is used as swap space.

Create a new partition

The second disk (/dev/sdb) on this test system is empty. To create a new partition run fdisk in its interactive mode:

sudo fdisk /dev/sdb

At the command prompt, type m to see the help menu or p to see the current partition list. To create a new primary partition, use the n command.

Enter p to create a primary partition then choose a partition number, in this case 1. Accept the default starting sector and then enter the size of the partition. On the test system, sdb is 100GB so I will create a 50GB partition by entering +50GB. Finally list the partitions using the p command. To save the partition table to the disk and exit, type w.

Create a new partition with fdisk

Deleting and setting the partition type

To delete a partition, use the d command. If the disk has multiple partitions, fdisk will ask which partition to delete, however if there is only one partition then fdisk will automatically delete it.

If you make a mistake at any point, use the q command to quit without saving. This will leave the hard disk in the same state as when you started fdisk.

Each partition needs to have a partition type. The partition type for Windows is different to the partition type for Linux and so on. There are also partition types for swap space and for older versions of Windows (before XP) using FAT rather than NTFS. Other Unix-like operating systems such as FreeBSD, OpenBSD or Mac OS X all have their own partition ids.

To see a list of partition types, use the l command. All the numbers listed are in hexadecimal, for example FreeBSD uses a5. Linux uses id 83 and Windows (from XP onwards) uses 7. If the partition is for use within your Linux installation, leave the partition type as the default 83, but if you want a partition that can be read by multiple operating systems including Windows then you should use either 7 or b.

To change the id on a partition, use the t command. You will be prompted for the partition number and then the partition code. If you have forgotten the code you wish to use, then you can type L, instead of entering a partition type, to see the list again. Once you have entered the partition code, use p to list the partitions and check that the partition type has been set as excepted.

Format and mount

Once a new partition has been created, it needs to be formatted. For partition types other than 83, it is best to format the partition using the relevant native operating system (i.e. Windows for id 7 etc). For Linux use the mkfs.ext3 or mkfs.ext4 commands for a typical partition:

sudo mkfs.ext4 /dev/sdb1

The filesytem then needs to be mounted using a command similar to this:

sudo mount /dev/sdb1 /home/gary/mediastore/

Where /home/gary/mediastore/ is the directory where you want the disk mounted. Finally the /etc/fstab file needs editing, for more information please read Getting to know your fstab.


fdisk is a versatile tool however make sure you backup your data before manipulating the partition table as mistakes can be costly. It is also worth noting that fdisk has some limitations, namely it does not understand GUID partition tables (GPTs) and it is not designed for large partitions. In these cases, use the parted tool.

Gary Sims

Gary has been a technical writer, author and blogger since 2003. He is an expert in open source systems (including Linux), system administration, system security and networking protocols. He also knows several programming languages, as he was previously a software engineer for 10 years. He has a Bachelor of Science in business information systems from a UK University.

Subscribe to our newsletter!

Our latest tutorials delivered straight to your inbox