Fdisk(fixed disk or format disk) is a text-based command-line utility for viewing and managing hard disk partitions on Linux. Using fdisk you can view, create, resize, delete, change, copy and move partitions.
Fdisk allows you to create a maximum of four primary partitions as permitted by Linux with each requiring a minimum size of 40mb. You can also have a much larger number of logical partitions by subdividing a primary partition.
In this article, let us review how to use fdisk command to manage disk partitions by employing some very simple and practical examples.
Important points to note
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 partition in the system. The commands list the partitions on your system. When you have more than one disk available, the partitions list are 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 -
a toggle a bootable flag
b edit bsd disklabel
c toggle the dos compatibility flag
d delete a partition
l list known partition types
m print this menu
n add a new partition
o create a new empty DOS partition table
p print the partition table
q quit without saving changes
s create a new empty Sun disklabel
t change a partition's system id
u change display/entry units
v verify the partition table
w write table to disk and exit
x 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/sdb
2. 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 which 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 partitions, use the following command to check the size of 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
With this simple tutorial, you can manage your disks and partitions with Fdisk. Hope this has been useful. If you have any issue with anything above, let me know in the comment section below this article. Thank you!