The “fdisk” (fixed/format disk) command allows users to manage disk partitions. It is used to create, modify, and delete partitions on the operating system. System administrators and developers use the “fdisk” command to move or copy data from the old to the new disk.
This post will demonstrate the detailed usage of the fdisk command in Linux with various examples.
- How Does the “fdisk” Command Work in Linux?
- View the Total Size of the Partition
- View All Disk Partitions
- View Specific Disk Partition
- Create a New Hard Disk Partition
- Delete a Hard Disk Partition
Let’s start the “fdisk” command in Linux.
How Does the “fdisk” Command Work in Linux?
By default, the “fdisk” command is installed in all the Linux distributions. The basic syntax of the command is provided as below:
$ fdisk [options] device
In the above syntax, the “device” represents the system hard disk (i.e.,/dev/sda, /dev/sdb, and /dev/sdc). While the supported options can be viewed using the “–help” flag.
$ fdisk --help
Let’s practice the “fdisk” command in Linux.
Example 1: View the Total Size of a Partition
To view the size of the partition, the “s” option is used with the “fdisk” command by specifying the hard disk of the system:
$ sudo fdisk -s /dev/sda
The output shows that the disk size of the system is “20971520” bytes.
Example 2: View All Disk Partitions
To view all the disk partitions of the current system, the “l” option is used with the “sudo” privilege:
$ sudo fdisk -l
The output returns the sizes of the disk partitions in “bytes” format.
Example 3: View a Specific Disk Partition
To view the specific disk partition, specify the hard disk’s name with the “l” option. In our case, display the disk information of “/dev/sda” by executing the below script:
$ sudo fdisk -l /dev/sda
The output returns the complete information of the hard disk “/dev/sda” in three categories: “BIOS boot”, “EFI System” and “Linux filesystem”.
Alternatively, users can print all partition tables through the “P” option after executing the “fdisk” command:
$ sudo fdisk /dev/sda
Example 4: Create a New Hard Disk Partition
To create the new hard disk partition, the “n” option is used by specifying the name of the disk. Additionally, it requires the partition number that will assign to the created partition:
$ sudo fdisk /dev/sda
The output shows the default partition number and sector of the newly created hard disk as “4” and “41940992”.
Example 5: Delete a Hard Disk Partition
To delete the hard disk partition, the “d” option specifies the hard disk name “/dev/sda”. Additionally, it acquires the partition number to delete data inside it:
$ sudo fdisk /dev/sda
The output shows that type “1” to delete the first partition in the hard disk.
That is all from the “fdisk” command.
Conclusion
Linux offers the “fdisk” command to create, delete, and modify the partition in the operating system. Using the “fdisk” command very carefully with the “sudo” privilege is recommended. This article has briefly explained the usage of the “fdisk” command with all possible examples in Linux.