Fdisk command to create and delete partition in Linux Guide 2021

how-to-use-fdisk-command-to-create-and-delete-partition-in-Linux

fdisk command in Linux is used to create and delete partitions in Linux of Hardisk. Its know as format disk, You can use it for manipulating disk partition table as well.

With the help of fdisk command you can view, create, resize, delete, change, copy and move patitions on a hard drive.

You can create a maximum four primary partition and unlimited logical partition. Creating logical partition depends on the size of the hard disk.

You will get a very simple and user freindly text based menu driven interface.

You can use this tool for creating space for new partitions, organizing space for new drives, re-organizing an old drive and copying or moving data to new disks.

Article Contents:

View/List All Disk Partition in Linux

If you want to make changes on the hard disk, You must know the structure of this hard disk.

For Example, how many hard disks are connected with my system? how many partitions is the exit? what is the size of partitions? what is the filesystem of existing partition? and much more.

Now your turn to make point, use the fdisk command followed by -l option to display all connected hard disk, Partitions, size etc.

The partitions are displayed by their device’s names. For example: /dev/sda, /dev/sdb or /dev/sdc.

fdisk -l

[root@localhost ~]# fdisk -l
Disk /dev/sda: 32 GiB, 34359738368 bytes, 67108864 sectors
Units: sectors of 1 * 512 = 512 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disklabel type: dos
Disk identifier: 0xed248e8f

Device     Boot   Start      End  Sectors Size Id Type
/dev/sda1  *       2048  2099199  2097152   1G 83 Linux
/dev/sda2       2099200 67108863 65009664  31G 8e Linux LVM


Disk /dev/sdb: 8 GiB, 8589934592 bytes, 16777216 sectors
Units: sectors of 1 * 512 = 512 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes


Disk /dev/mapper/cl-root: 28.9 GiB, 31058821120 bytes, 60661760 sectors
Units: sectors of 1 * 512 = 512 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes


Disk /dev/mapper/cl-swap: 2.1 GiB, 2222981120 bytes, 4341760 sectors
Units: sectors of 1 * 512 = 512 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
[root@localhost ~]# 
[root@localhost ~]# fdisk -l
Disk /dev/sda: 32 GiB, 34359738368 bytes, 67108864 sectors
Units: sectors of 1 * 512 = 512 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disklabel type: dos
Disk identifier: 0xed248e8f

Device     Boot   Start      End  Sectors Size Id Type
/dev/sda1  *       2048  2099199  2097152   1G 83 Linux
/dev/sda2       2099200 67108863 65009664  31G 8e Linux LVM


Disk /dev/sdb: 8 GiB, 8589934592 bytes, 16777216 sectors
Units: sectors of 1 * 512 = 512 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes


Disk /dev/mapper/cl-root: 28.9 GiB, 31058821120 bytes, 60661760 sectors
Units: sectors of 1 * 512 = 512 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes


Disk /dev/mapper/cl-swap: 2.1 GiB, 2222981120 bytes, 4341760 sectors
Units: sectors of 1 * 512 = 512 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
[root@localhost ~]# 

In the above example, I had 2 devices connected to my Linux system, /dev/sda and /dev/sdb

View Partition on a Specific Disk

If you have multiple hard disk devices connected on your system, and you want to view all partitions of specific hard disk use the option ‘-l‘ followed by device name.

It may be /dev/sda or /dev/sdb or /dev/sdc etc. For example, I want to display all disk partitions of device /dev/sda, following syntax is perfect for me.

fdisk -l /dev/sda

[root@localhost ~]# fdisk -l /dev/sda
Disk /dev/sda: 32 GiB, 34359738368 bytes, 67108864 sectors
Units: sectors of 1 * 512 = 512 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disklabel type: dos
Disk identifier: 0xed248e8f

Device     Boot   Start      End  Sectors Size Id Type
/dev/sda1  *       2048  2099199  2097152   1G 83 Linux
/dev/sda2       2099200 67108863 65009664  31G 8e Linux LVM
[root@localhost ~]# 

Check all Available fdisk Commands

As I have told you before in this article fdisk command in Linux has text based menu driven interface, It is necessary that I must know the menus/commands.

To see all the command which are available under fdisk command you can use /dev/sda partition with fdisk command.

When you use specific device, then you can type “m” keyword and hit enter all the command/menus/options will print in the terminal.

[root@localhost ~]# fdisk /dev/sda

Welcome to fdisk (util-linux 2.32.1).
Changes will remain in memory only, until you decide to write them.
Be careful before using the write command.


Command (m for help): m

Help:

  DOS (MBR)
   a   toggle a bootable flag
   b   edit nested BSD disklabel
   c   toggle the dos compatibility flag

  Generic
   d   delete a partition
   F   list free unpartitioned space
   l   list known partition types
   n   add a new partition
   p   print the partition table
   t   change a partition type
   v   verify the partition table
   i   print information about a partition

  Misc
   m   print this menu
   u   change display/entry units
   x   extra functionality (experts only)

  Script
   I   load disk layout from sfdisk script file
   O   dump disk layout to sfdisk script file

  Save & Exit
   w   write table to disk and exit
   q   quit without saving changes

  Create a new label
   g   create a new empty GPT partition table
   G   create a new empty SGI (IRIX) partition table
   o   create a new empty DOS partition table
   s   create a new empty Sun partition table


Command (m for help):

You want print all partition table of hard disk, then you must be on command mode of specific hard disk say /dev/sda. Use following command to select the hard disk and get command mode.

#fdisk /dev/sda
OR
$ sudo fdisk /dev/sda

From the command mode, enter ‘p‘ instead of ‘m‘ as we did earlier. As I enter ‘p‘, it will print the specific /dev/sda partition table.

Command (m for help): p

[root@localhost ~]# fdisk /dev/sda

Welcome to fdisk (util-linux 2.32.1).
Changes will remain in memory only, until you decide to write them.
Be careful before using the write command.


Command (m for help): p
Disk /dev/sda: 32 GiB, 34359738368 bytes, 67108864 sectors
Units: sectors of 1 * 512 = 512 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disklabel type: dos
Disk identifier: 0xed248e8f

Device     Boot   Start      End  Sectors Size Id Type
/dev/sda1  *       2048  2099199  2097152   1G 83 Linux
/dev/sda2       2099200 67108863 65009664  31G 8e Linux LVM

Command (m for help):

fdisk command to create a partition in Linux

If you have new hard disk connected on the system, want to use it. You will have to create partition first. You can’t store data inside hard disk without creating partition.

If you’ve free space left on one of your device say /dev/sdb (old hard disk connected to the system) and would like to create a new partition under it.

Then you must be select hard disk first by using following command:

#fdisk /dev/sdb

[root@localhost ~]# fdisk /dev/sdb 

Welcome to fdisk (util-linux 2.32.1).
Changes will remain in memory only, until you decide to write them.
Be careful before using the write command.

Device does not contain a recognized partition table.
Created a new DOS disklabel with disk identifier 0xc11739be.

Command (m for help): 

When you access the command mode of /dev/sdb, now press “n” command to create a new partition under /dev/sdb with specific size. You can see in the following result.

When you are creating a new partition, You will have to select one of two options ‘extended‘ or ‘primary‘ partition creation.

Press ‘e‘ for extended partition and ‘p‘ for primary partition. Now you will enter following two inputs.

  • First cylinder number of the partition to be create.
  • Last cylinder number of the partition to be created (Last cylinder, +cylinders or +size).

You can enter the size of cylinder by adding “+5000M” in last cylinder. Here, ‘+‘ means addition and 5000M means size of new partition (i.e 5000MB).

[root@localhost ~]# fdisk /dev/sdb 

Welcome to fdisk (util-linux 2.32.1).
Changes will remain in memory only, until you decide to write them.
Be careful before using the write command.

Device does not contain a recognized partition table.
Created a new DOS disklabel with disk identifier 0xc11739be.

Command (m for help): n
Partition type
   p   primary (0 primary, 0 extended, 4 free)
   e   extended (container for logical partitions)
Select (default p): p
Partition number (1-4, default 1):  
First sector (2048-16777215, default 2048): 
Last sector, +sectors or +size{K,M,G,T,P} (2048-16777215, default 16777215): +5000MB

Created a new partition 1 of type 'Linux' and of size 4.7 GiB.

Command (m for help): w
The partition table has been altered.
Calling ioctl() to re-read partition table.
Syncing disks.

[root@localhost ~]# 

Please keep in mind that after creating a new partition, you should run ‘w‘ command to alter and save new changes to partition table and finally reboot your system to verify newly created partition.

[root@localhost ~]# fdisk -l /dev/sdb 
Disk /dev/sdb: 8 GiB, 8589934592 bytes, 16777216 sectors
Units: sectors of 1 * 512 = 512 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disklabel type: dos
Disk identifier: 0xc11739be

Device     Boot Start     End Sectors  Size Id Type
/dev/sdb1        2048 9766911 9764864  4.7G 83 Linux
[root@localhost ~]# 

mkfs command to format a Partition in Linux

When you create a new partition, what would be the next step. Can you store data after creating partition?

No, you can’t store data inside new partition whithout formating it. So You can use mkfs command to format the newly created partition.

Type the following command in the terminal to format a partition. Here /dev/sdb1 is my newly created partition.

mkfs.ext4 /dev/sdb1

[root@localhost ~]# mkfs.ext4 /dev/sdb1
mke2fs 1.44.6 (5-Mar-2019)
Creating filesystem with 1220608 4k blocks and 305216 inodes
Filesystem UUID: 702bd125-5407-4546-965a-e171e63b927d
Superblock backups stored on blocks: 
	32768, 98304, 163840, 229376, 294912, 819200, 884736

Allocating group tables: done                            
Writing inode tables: done                            
Creating journal (16384 blocks): done
Writing superblocks and filesystem accounting information: done 

[root@localhost ~]# 

Check the Size of Partition

All done with new created partition. You have created a partition and want to know the partition size. After formatting new partition, check the size of that partition using flag ‘s‘ (displays size in blocks) with fdisk command.

See the example below

fdisk -s /dev/sdb1

[root@localhost ~]# fdisk -s /dev/sdb 
8388608
[root@localhost ~]#  

Delete a partition in Linux by fdisk command

“d” option is used to delete the partition. I have created new partion in linux size of 5GB in previous section. This partition name is /dev/sdb1. I want to delete it.

This partition belongs to /dev/sdb hard disk. So first I have to enter fdisk command mode with this device. I am going to use the following command.

You can follow the same steps. Be attentive at the time of hard disk selection.

#fdisk /dev/sdb

Next, enter ‘d‘ keyword to delete any given partition name from the system.

When you enter ‘d‘, You may ask to enter partition number. As I want to delete from /dev/sdb hard disk.

Suppose I want to delete /dev/sdb1 then I must enter number ‘1‘ here. it will delete partition number ‘1‘ (i.e. /dev/sdb1) disk and shows free space in partition table.

[root@localhost ~]# fdisk /dev/sdb

Welcome to fdisk (util-linux 2.32.1).
Changes will remain in memory only, until you decide to write them.
Be careful before using the write command.


Command (m for help): d
Selected partition 1
Partition 1 has been deleted.

Command (m for help): w
The partition table has been altered.
Calling ioctl() to re-read partition table.
Syncing disks.

[root@localhost ~]# 

Do not forget to enter ‘w‘ to write a table to disk and exit after making new alterations to the partition table. The new changes would only take place after the next reboot of the system.

[root@localhost ~]# fdisk -l /dev/sdb
Disk /dev/sdb: 8 GiB, 8589934592 bytes, 16777216 sectors
Units: sectors of 1 * 512 = 512 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disklabel type: dos
Disk identifier: 0xc11739be
[root@localhost ~]# 

Conclusion

I’ve tried my best to cover most of the basic uses of fdisk command in Linux, but still, fdisk contains a variety of other expert commands you can use them by entering ‘x‘.

For more detailed information, you can check the manual page. To display the manual page use man command from the terminal.

If I’ve missed any important command, please do share it with me via the comment section.

If you like our content, please consider buying us a coffee.
Thank you for your support!