fdisk (meaning format disk or fixed disk) is the command line-based utility commonly used for viewing and manipulating hard disk partitions in linux. Fdisk is a powerful utility using which you can view, generate, delete, resize, change, move and copy hard disk partitions using the command line interface.
This fdisk utility is very much advantageous at the time of forming some space for new partitions, organizing the space for newly created drives, re-organizing the older partitions and copying/moving the data to other partitions. Using fdisk you can create at most four new primary partitions and several logical partitions, depending upon the hard disk space available in the system.
This article will go through basic usage of fdisk command that helps to manage partition tables in Linux systems. In order to run this command successfully, you need to have root privileges. Adding a word of caution, do not use this command if you don’t know what you are willing to do or it will create a huge mess!
1. Enlisting all Disk PartitionsThe most fundamental use of fdisk will be for viewing all the disk partitions of your system and in this regard, option –l is used. This will enlist all the disk partitions in your system along with their device names like /dev/sda, /dev/sdb/and so on.
Syntax: sudo fdisk –l
2. Viewing Specific Disk PartitionTo observe specific disk partition, you will have to use option –l followed by the device name (/dev/sda, /dev/sdb or any other partition) you wish to observe.
Syntax: sudo fdisk –l <device_name>
Example: sudo fdisk –l /dev/sda
3. Command Mode UsageTo observe all the options associated with fdisk command, use it without any option (but do not forget to mention device name in the command).
Pressing m key will display all the possible commands that can be applied on the mentioned disk partition.
4. Printing the Partition TableTo print the current partition table of the system using command mode, just follow similar procedure but press p key instead. It will print the list of partitions available in the hard disk.
5. Deleting the Partition
In order to delete any partition, option d in the command mode can be used. When d is pressed, it expects the number of partition (ID of the partition which can be observed using option p) that you want to delete. When you check the partition table upon deleting the partition, deleted partition doesn’t appear there, but the changes remain unsaved unless they are written using w key.
6. Creating the Partition
If you are having free space on any one of your devices and you wish to create a new partition under it, you can do this using option n in the command mode.
At the time of creating the partition you will be asked whether you need primary partition or logical (or extended) partition. Press p for primary partition and l for the logical one. As said earlier, you may create several logical partitions but a maximum of four (4) primary partitions.
You will now need to specify the sector the new partition would start at and the last sector where the partition will end. While specifying the last sector, you can mention the total size that would be occupied by the partition, say for a partition of 50 Gigabytes, you will have to enter “+50G”. Remember, if you don’t use any unit (G for Gigabytes in this case), ‘sectors’ will be considered as the default unit.
And again, changes won’t take place unless they are saved. Press w to save all the changes to the partition table and reboot the system.
7. Formatting the Partition
When the new partition is formed, it is recommended that the user should format the newly created partition with a file system which can be done with the use of ‘mkfs’ command. So as to format a newly created partition, Enter the following command:
Syntax: mkfs.ext4 <partition_name>
Example: mkfs.ext4 /dev/sda6
8. Checking the Partition SizeTo check size of any specific partition, use the option –s which will display the disk space occupied by the partition in terms of blocks.
Syntax: fdisk –s <partition_name>
Example: fdisk –s /dev/sda2
9. Fixing Partition Table Order
When a logical partition is deleted and created again, you may face an error message saying 'Partition out of order' or 'Partition table entries are not in disk order'.
When you delete some of the partitions (say sda1, sda2 and sda3) and create a new partition which you suppose to take a name as 'sda1', system would create it with a name 'sda4'. Because, when you delete the partitions, sda4 will be renamed as sda1, sda5 as sda2 and so on, shifting the free space at the end.
To solve the problems associated with partition table order, option x (extra functionality section) can be used followed by option f (fix the partition table order) in the command mode. Do not forget to save the changes using option w.
10. Disabling Boot Flag
Generally, when fdisk is run, it will show a boot flag symbol i.e. (*) on every partition. To enable/disable this boot flag, follow the steps mentioned below:
When you see current partition table (using p command), there is a boot flag on /dev/sda1 and to enable/disable that, option a can be used.
That’s it! In this article, I tried to cover basic usage of fdisk command. But this is not the end, fdisk has several expert commands which can be used using option x.