Linux fdisk command

The fdisk command in Linux is used for disk partitioning. It allows you to create, modify, and delete partitions on a hard disk. The fdisk command is typically run with administrative privileges (e.g., as root or using sudo) and requires caution as it directly affects the disk’s partition table.

Here’s an overview of how to use the fdisk command:

  1. Selecting the Disk:
    To start using fdisk, specify the disk device as an argument to the command. For example, to work with the first hard disk (/dev/sda), use:
   sudo fdisk /dev/sda
  1. Interactive Command Prompt:
    Once you’re in the fdisk utility, you’ll be presented with an interactive command prompt. You can issue various commands to manage the partitions on the selected disk.
  2. Common fdisk Commands:
  • p: Print the partition table, showing the existing partitions on the disk.
  • n: Create a new partition.
  • d: Delete an existing partition.
  • t: Change the partition type.
  • l: List the available partition types.
  • w: Write the changes and exit.
  • q: Quit without saving changes.
  1. Creating a New Partition:
    To create a new partition, use the n command followed by the desired partition number, starting sector, and size. You can specify the size in sectors, cylinders, or using a specific size unit (e.g., M for megabytes or G for gigabytes). For example:
   Command (m for help): n
   Partition type:
    p primary (0 primary, 0 extended, 4 free)
    e extended
   Select (default p): p
   Partition number (1-4, default 1):
   First sector (2048-41943039, default 2048):
   Last sector, +sectors or +size{K,M,G} (2048-41943039, default 41943039):
  1. Deleting a Partition:
    To delete an existing partition, use the d command followed by the partition number. For example:
   Command (m for help): d
   Partition number (1-4): 1
  1. Changing the Partition Type:
    To change the type of a partition, use the t command followed by the partition number. You’ll be prompted to select the partition type from a list. For example:
   Command (m for help): t
   Partition number (1-4): 1
   Hex code or GUID (L to show codes, Enter = 8300): L
  1. Saving Changes and Exiting:
    Once you have made the desired changes, use the w command to write the changes to the disk’s partition table and exit fdisk. For example:
   Command (m for help): w
  1. Examples:
  • Create a new primary partition on /dev/sda:
sudo fdisk /dev/sda
n
p
[default options]
w

Delete an existing partition on /dev/sda:

sudo fdisk /dev/sda
d
[partition number]
w
Change the type of a partition on /dev/sda:
```
sudo fdisk /dev/sda
t
[partition number]
[desired partition type]
w