Signup/Sign In

fdisk Command In Linux With Examples

fdisk also known as format disk is a dialog-driven tool in Linux used for generating and altering disk partition tables. It is used for the view, create, remove, alter, resize, copy and relocate partitions on a hard disk using the dialog-driven interface.

fdisk enables you to create a maximum of four main partitions and the number of the logical partition depends on the size of the hard disk you are using. It lets the user:

  • To Create room for additional partitions.
  • Organizing space for new drives.
  • Re-organizing old drives.
  • Copying or Moving data to new disks(partitions).

Synopsis:

fdisk [options] device

or

fdisk -l [device...]

1. View All Disk Partitions:

The first thing to do before doing anything with the disks and partition is to see basic facts about all possible partitions in the system. The following command is used to list the partitions on your system and view their /dev names. For example /dev/sda, /dev/sdb or /dev/sdc.

$ sudo fdisk -l


2. Examine Partition on a Specific Disk:

The below command is used to view all disk partitions on device /dev/sda.

$ sudo fdisk -l /dev/sda


3. View all fdisk Commands:

To check all the commands which are available under fdisk command you may utilize /dev/sda partition with fdisk command.

$ sudo fdisk /dev/sda


Note: This will ask for a command. Type m for displaying all the operations which may conduct on /dev/sda. After hitting m you will get:

4. Create a Hard Disk Partition:

For this go inside the hard drive partition that is the /dev/sda partition, and run the following command:

$ sudo fdisk /dev/sda

Now you have to enter n to create a new partition and then type p for establishing a primary partition and e for generating an extended or logical partition depending on the kind of partition to construct.

Run w command to write the modifications and reboot your system.

5. Delete a Hard Drive Partition:

To delete a partition for the hard disk and free up space consumed by that partition for example /dev/sdb. Go to the command menu using following:

$ sudo fdisk /dev/sda

and then type d to proceed to the remove partition menu. It will request the partition number you wish to delete(type the number).

Run w command to write the modifications and reboot the system.

6. How to see the size of your Partition:

$ sudo fdisk -s /dev/sda


Note:

To check for the manual page of fdisk command, run the following command:

$ man fdisk

To get the help message and listing of all options, use the following command option:

$ sudo fdisk -h