Important points to note
Fdisk requires root to operate. On Debian and Ubuntu based systems, you can prefix with sudo. On other distros, use su- command to get a root shell and then enter the commands without sudo.Take note of the form the device names (disks) are presented. Usually, you see the following names
/dev/hd[a-h] for IDE disks /dev/sd[a-p] for SCSI disks /dev/ed[a-d] for ESDI disks /dev/xd[ab] for XT disks
3. Warning: Do not delete, modify, or add partition unless you know exactly what you are about. You ran the risk of losing your data!
1. View All Existing Disk Partitions
The first thing you want to do before tampering with your disks and partition is to view basic details about all available partitions in the system. The commands list the partitions on your system. When you have more than one disk available, the partitions list is ordered by the device’s /dev name as in /dev/sda, /dev/sdb and so on. Use the following command to view all existing partitions in the system –
$ sudo fdisk -l
2. View Partition on a Specific Disk
If you want to look at all the partitions on a specific disk, use the following command to view all disk partitions on device /dev/sdb.# sudo fdisk -l /dev/sdb
3. View all fdisk Commands
You can use the following command to view all available fdisk – # sudo fdisk /dev/sda This will provide you with a prompt. Enter m to see the list of all available commands of fdisk which can be operated on /dev/sda. Command (m for help): m The output will be this –Command actiona toggle a bootable flagb edit bsd disklabelc toggle the dos compatibility flagd delete a partitionl list known partition typesm print this menun add a new partitiono create a new empty DOS partition tablep print the partition tableq quit without saving changess create a new empty Sun disklabelt change a partition’s system idu change display/entry unitsv verify the partition tablew write table to disk and exitx extra functionality (experts only)
4. Delete a Hard Disk Partition
Assuming you need to combine some partitions to form a larger one, you will first have to delete those partitions. To delete a specific partition on /dev/sdb2, follow the following -1. Enter the following command to enter/view the disk –# sudo fdisk /dev/sdb2. Enter ‘d’ to delete a partition.Command (m for help): d 3. You will be prompted to enter the partition number. We enter 2 in our case to delete /dev/sdb2. Save changes by entering ‘w’ at command and reboot for changes.
5. Create a New Disk Partition with Specific Size
To create a new partition, enter the following command -1. Enter the following command to enter/view the disk# sudo fdisk /dev/sdb 2. Enter ‘n’ to create a new partition that will prompt you to specify for either a primary partition or an extended partition. Enter ‘p’ for a primary partition or ‘e’ for an extended partition. 3. You will then be prompted to enter the first cylinder or sector number of the partition to be created. You may press Enter to accept the defaults. 4. You will then be prompted to enter the Last cylinder or sector number of the partition to be created. You may press Enter to use all available space after the first sector or enter a specific size such as +2G or +256M for a 2 gigabyte or 256 megabyte partition respectively. 5. Run ‘w’ command to write the changes and reboot your system.
6. How to Format a Partition?
After you have created your partition, you need to format it with a file system. Use the following command to format to ext4 file format –
$ sudo mkfs.ext4 /dev/sdb1
7. How to View the Size of your new or existing Partition?
If you want to check the size of a specific partition, use the following command to check the size of the partition on /dev/sda2.
$ sudo fdisk -s /dev/sda2
8. Viewing the Partition Table
You can use the following command to look at your current partition table –Use ‘p’ in command mode. Command (m for help): p
Conclusion
With this simple tutorial, you can manage your disks and partitions with Fdisk. Hope this has been useful. If you have any issues with anything above, let me know in the comment section below this article. Thank you!