Use of fdisk command to manage disk partition in Linux

fdisk is terminal/text based command used in Linux to manage disk partition. fdisk command can be used to create partition, delete partition and change existing partitions.

Understanding partition

Partitions are of three basic types

  1. Primary partition – Primary partition type was limited from very beginning. A hard disk could only accommodate four primary partitions. The information of primary partition is held in boot sector and can hold details for only four primary sector
  2. Extended partition – To overcome limitation of primary partition Extended partition were developed. Only one extended partition can be there in a disk. This extended partition is further subdivided into logical partitions.
  3. Logical partition – Logical partition are subdivision of extended partitions.

Use of fdisk command in Linux

List / View all disk in Linux system

This command is used to list all available disks in Linux. All partitions are listed as their device names.

Syntax

fdisk –lUse of fdisk command in Linux

Manage a disk device

To make changes to partition table of a disk you will have to enter to command mode.
Syntax

 fdisk  [device name]

Example

fdisk /dev/sda

For most of the commands you will have to enter in command mode first.

View all available commands in fdisk

This command can be used in command mode for fdisk. Enter into command mode with one of the disks selected and type ‘m’ at prompt. You will be presented by available commands for fdisk.

Use of fdisk command in Linux

Viewing partition table in fdisk

To view partition table again enter into command mode with disk device selected and type ‘p’ at prompt. You will be presented with details of partition table for selected disk.

Create new partition with fdisk command

To create new partition, firstly enter into command mode with reference to target disk. In command mode press ‘n’. You will be prompted with option for creation of new partition. The option will be for creation of extended or primary partition. For creation of extended partition you will have to press ‘e ‘ and for primary partition you will have to press ‘p’.

Now you will be asked to provide First cylinder number, you can press enter to use default value. After this you will asked to input Last cylinder number for partition to be created, again you can press enter to use default value.

Now you will have finalize all changes done by using write command (press ‘w’ at command fdisk prompt)

Delete partition with fdisk command

Using d at prompt in fdisk command mode will delete partition from disk. After pressing ‘d’ you will be prompted to enter partition number. Provide the disk which you want to delete e.g. 5. After deletion check your partition table by pressing p. The disk might appear deleted but fdisk will finalise changes after you press w.

Use of fdisk command in Linux