Take Control of Linux Disk Partitions with These 10 fdisk Commands
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 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:
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 , 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:
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.
If you find the displayed information a bit tough to digest, compare it with a disk space visualization tool 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:
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:
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 .
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).
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:
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 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!
Affiliate Disclosure: By buying the products we recommend, you help keep the site alive. Read more.