fdisk Command in Linux



The fdisk command in Linux creates and manipulates the disk partition table. It allows system administrators to manage disks. The fdisk tool is a powerful command line utility that allows creating, deleting, and resizing disk partitions.

Table of Contents

Here is a comprehensive guide to the options available with the fdisk command −

Syntax of fdisk Command

The syntax of the Linux fdisk command is as follows −

fdisk [options] [device]

The [options] field is used to specify the options to modify the command’s behavior. The [device] field is used to specify the device that needs to be manipulated using fdisk.

fdisk Command Options

The options of the fdisk command are listed below −

Flags Options Description
-b size --sector-size To specify the physical or logical sector size
-B --protect-boot To skip erasing of the first disk sector when creating a new label (contains boot bits)
-c= mode --compatibility To specify the compatibility mode (dos or nodos)
-L= when --color To colorize the output (when can be auto, always, never)
-l --list To display the partitions
-x --list-details To display partitions with details
-n --noauto-pt To skip the creation of a partition table on empty devices
-o list --output To specify the column to print
-t type --type To specify the specific partition table type only
-u unit --unit To display units (cylinders or sectors)
-s --getsz Display the partition size blocks (deprecated)
--bytes To display the size in bytes instead of human-readable format
--lock=mode To use the exclusive BSD lock (yes, no or nonblock)
-w when --wipe To wipe the filesystem, RAID, and partition table signature (auto, always, and never)
-W when --wipe-partition To wipe the filesystem, RAID, and partition table signature from newly created partition (auto, always, and never)
-C number --cylinders To specify the number of cylinders of the disk
-H number --heads To specify the number of heads (reasonable values are 255 and 16)
-S number --sectors To specify the number of sectors per track of the disk (a reasonable value is 63)
-h --help To display the help
-V --version To display the command version

Examples of fdisk Command in Linux

This section demonstrates the usage of the fdisk command in Linux with examples −

Note − The fdisk command needs sudo privileges.

  • Listing Partitions
  • Listing Partition of a Specific Device
  • Listing Partition with Details
  • Resizing a Partition
  • Creating a Partition
  • Deleting a Partition
  • Display the Partition Size

Listing Partitions

To list all the partitions along with unit and sector size information, use the -l or --list option −

sudo fdisk -l
Listing Partitions Using fdisk command

Listing Partition of a Specific Device

To list the partition of a specific device, use the -l or --list option along with the device name −

sudo fdisk -l /dev/vda
Listing Partition of a Specific Device

Listing Partition with Details

To display the partitions with the details such as starting/ending sectors, type-UUID, name, and attributes, use the -x or --list-details option −

sudo fdisk -x

Resizing a Partition

To resize a partition, use the fdisk command with the target device name. For example, to resize a partition of /dev/vda1, first unmount it using the command given below −

sudo umount /dev/vda1

Now, create the partition by executing the fdisk command in the following way −

sudo fdisk /dev/vda

First, the command will prompt for help. To display the sub-commands of the fdisk tool, type m and press Enter.

To continue to create the partition type n and press Enter −

Resizing a Partition Using fdisk

Now, select the partition type from the primary and extended options by typing p or e.

Partition Type n and Press Enter

Select the partition number −

Select the Partition Number

Now specify the first and last sectors. Press Enter to select the default sector sizes. The output will confirm that the partition has been created. Now, type w to apply the changes.

Type w to Apply the Changes

Lastly, reboot the system.

sudo reboot

Creating a Partition

To create the partition, specify the disk name after the fdisk command −

sudo fdisk /dev/vda

All prompts will be similar to the prompts that appear while resizing the partition.

Deleting a Partition

To delete the partition, run the fdisk command along with the device name.

sudo fdisk /dev/vda

Now, type the d sub-command and then type the partition number to delete. After that, type the partition number to be deleted. Once the partition number is typed, it will immediately be deleted. To apply the changes, type w and reboot the system.

Deleting a Partition using fdisk

Display the Partition Size

To display the size of the partition in blocks, use the -s or --getsz option −

sudo fdisk -s /dev/vda1
Display Partition Size Using fdisk

Conclusion

The fdisk command on Linux is used to create, delete, and resize the disk partitions. It is an interactive utility that is easy to use for disk management. It offers a robust and versatile way of managing disk partitions on Linux systems.

In this tutorial, we covered the fdisk command, its syntax, options, and usage in Linux with examples.

Advertisements