Take Control of Linux Disk Partitions with These 10 fdisk Commands

Christian Cawley 08-01-2016

Managing partitions and disks on Windows and OS X is reasonably straightforward, thanks to the GUI tools that give you direct, mouse-driven access. While desktop disk management tools are also available for Linux distros (such as the Disk Utility in Ubuntu), using the fdisk command line tool 4 Ways to Teach Yourself Terminal Commands in Linux If you want to become a true Linux master, having some terminal knowledge is a good idea. Here methods you can use to start teaching yourself. Read More offers more options.


And if you’re new to Linux, you’ll also benefit from spending some time using Terminal to input commands “old school”.

What Is fdisk?

A widely-used command line tool, fdisk (short for “format disk” or “fixed disk”) enables you to create, delete, resize, change, copy and move partitions on any hard disk drive connected to a Linux PC. Data can also be moved to other disks, and up to four primary partitions can be created (depending on the size of the HDD).

You can get started using the fdisk command by opening up a Terminal window and typing


The resulting usage instructions will give you an idea of how to use fdisk. To help you further, we’ve compiled the following list of examples of how you can use fdisk to manage hard disk drives on your Linux PC.

Find Your Disk

To get started, you’ll need to list the devices attached to your PC, and the partitions setup on them already. In Terminal, enter:

fdisk -l

The resulting list should display devices and partitions, typically named and numbered, e.g. /dev/hda1. However if you’re using an additional device, like flash storage (perhaps a USB memory stick USB Flash Drive Guide: 5 Things to Know When Buying One USB flash drives might seem mundane and straightforward, but there are a few tips and quirks that you should know before you buy your next one. Read More , or digital camera), you will probably see /dev/sdb.

(As you might notice from the naming convention, hda refers to “hard disk, device a”; similarly sdb refers to “SD/flash, device b”.)

Disk-Specific fdisk Commands

Not all commands are available to all disks. To see which commands you will be able to use with the disk you’re managing, begin by selecting the disk:

fdisk /dev/sda

Linux will display a brief instruction to tap m for help, which will result in the following command actions being listed:



These are the actions that can be used with the selected disk.

Print Partition Table Details

After selecting your disk, you can print to screen a complete list of the partition table. This gives you information about the selected device or partition’s capacity, the size and number of disk sectors and cylinders, and the identifier.



Display this information by simply entering p.

fdisk /dev/sda

If you find the displayed information a bit tough to digest, compare it with a disk space visualization tool Linux Disk Space: How to Visualize Your Usage Even with terabyte drives becoming the norm, it's amazing just how fast all of that space can fill up. What if you could see your entire disk space usage at a glance? Read More for easier understanding.

Create & Delete Disk Partitions

As per the table of commands that you uncovered earlier, you should be able to simply create and delete partitions as required. Again, if you haven’t already, begin by selecting the device:

fdisk /dev/sda

To create a new partition, enter


then either e for and extended partition, or p for a primary partition.

After this, you’ll be prompted to indicate the first and final cylinders of the new partition. This can be confusing, especially if you’re not sure about how many cylinders are required; fortunately, you can also enter the size of the partition you want, such as +2000M for a 2000 MB (2 GB) partition. In some versions of Linux, defaults are displayed; tap Enter to accept these and save time.


Once you’ve done this, confirm the action by entering:


This will update the partition table.

To delete a partition in the command line, select the device:

fdisk /dev/sda

Next, tap the delete command, d


You’ll be prompted to enter a number, 1-4, so input the number that corresponds to the partition you wish to delete. Complete the action by entering w to update the partition table. Of course, there are other ways to resize a partition in Linux Three Ways to Resize a Linux Partition Safely How do you make changes to hard drive partitions without breaking your system and losing all of your files? Start with Linux! Read More .

Troubleshooting Partition Table Order

It’s not unheard of to find a “partition out of order” message after deleting one or more Linux disk partitions. This might happen on a system with four partitions, where number 3 is deleted. You might expect to be able to create a new number 3, but what actually happens is that number 4 is automatically renamed to 3, with the free space moved to the end.

This causes the error message ‘Partition table entries are not in disk order’.

To fix this, select the disk and input x to use extra functionality (see the list above).

fdisk /dev/sda

With this done, enter the f command to fix the partition table order.


Again, finish this off by entering w to write the updated table to disk and exit.

Format Your Linux HDD

With a new partition setup, you can quickly the format it with the mkfs command:

mkfs.ext4 /dev/sda4

This is broken down into command.filesystem /device/device name. You can number the device yourself, but it needs to be in numerical order – that is, don’t add sda4 if you don’t have an sda3.

Once you’ve done this, it is worth just checking the size of the partition. You can do this with the -s flag:

fdisk -s /dev/sda2

You’ll see the size of the partition displayed in bytes, so you’ll need to do a little bit of mental arithmetic to round it up to a more comfortable figure Memory Sizes Explained: Gigabytes, Terabytes, and Petabytes in Context How big is a gigabyte? What is a terabyte? Will you ever need a petabyte? Let's explore computer storage sizes in perspective. Read More in megabytes or gigabytes.

We’ve demonstrated over 10 fdisk commands, but which would you include in such a list? Is there anything you think should be included? Or have you eschewed fdisk completely in favor of GUI tools?

Tell us in the comments!

Related topics: Disk Partition, Hard Drive, Linux, Terminal.

Affiliate Disclosure: By buying the products we recommend, you help keep the site alive. Read more.

Whatsapp Pinterest

Leave a Reply

Your email address will not be published. Required fields are marked *

  1. Ranjith Prabhu
    February 6, 2017 at 12:38 am

    What is the command to format an sd card as a whole instead of formating it partition wise.

  2. Anonymous
    January 10, 2016 at 6:34 pm

    "(As you might notice from the naming convention, hda refers to 'hard disk, device a'; similarly sdb refers to 'SD/flash, device b'.)"

    SDA and SDB can also (and usually do) refer to SATA devices, not just removable or flash.

    • Christian Cawley
      January 10, 2016 at 8:07 pm

      Thanks Howard, appreciate that correction.

  3. Anonymous
    January 9, 2016 at 2:15 am

    Good tutorial. Love the commandline.

    Try gdisk! It seems more up to date, as it supports newer file systems and partition layouts. It also is almost just as easy to use.

    Oh and as a precaution, could the 'sda's be changed to something other than the primary drive just in case some script kiddie starts complaining about accidentally wiping his Linux box?

    Some distros will allow you to wipe a huge chunk of your drive without you even realizing it until you try to reboot!! (Speaking from experience lol.)

    • Christian Cawley
      January 10, 2016 at 8:08 pm

      Thanks Robin, pleased you enjoyed it. I'll look into the points you make. Hope it wasn't too painful!

  4. Anonymous
    January 8, 2016 at 5:09 pm

    "such as the Disk Utility in Ubuntu"
    One can also use GParted, either bundled with many distributions or standalone. The advantage of the standalone is that it can be used for Linux, BSD, Windows and Mac filesystems.

    • Christian Cawley
      January 10, 2016 at 8:08 pm

      It's a fair point, and I think we've covered gparted previously. Will investigate whether it is worth revisiting.