(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.
- 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 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!
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 –
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.
You can use the following command to view all available fdisk –
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 –
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 –
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.
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.
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 –
If you want to check the size of a specific partitions, use the following command to check the size of partition on /dev/sda2.
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!