How to Create and Manage Disk Partitions in Linux

The fdisk command in Linux is a powerful tool for managing disk partitions. It allows you to create, delete, resize, and modify partitions on storage devices.

Alby Andersen

The fdisk command in Linux is a powerful disk partitioning tool used to create, delete, and modify disk partitions. It is commonly used to manage disk drives and partitions on a system. While fdisk works primarily with MBR (Master Boot Record) partition schemes, it also supports GPT (GUID Partition Table) on modern systems.


List All Disks and Partitions

sudo fdisk -l  


Displays information about all disks and their partitions.


Start fdisk for a Specific Disk

sudo fdisk /dev/sda  


Opens the fdisk interface for /dev/sda.


Create a New Partition

  1. Start fdisk for the disk:
   sudo fdisk /dev/sda  
  1. Press n to create a new partition.
  2. Follow the prompts to specify the partition type (primary/extended), size, and location.
  3. Press w to write changes and exit.

Delete a Partition

  1. Start fdisk for the disk:
   sudo fdisk /dev/sda  
  1. Press d to delete a partition.
  2. Select the partition number to delete.
  3. Press w to save changes.

Change Partition Type

  1. Start fdisk for the disk:
   sudo fdisk /dev/sda  
  1. Press t to change the partition type.
  2. Enter the partition number and the type code (e.g., 83 for Linux, 8e for LVM).
  3. Press w to save changes.

Display Partition Table

  1. Start fdisk for the disk:
   sudo fdisk /dev/sda  
  1. Press p to print the partition table.

Resize a Partition

  1. Start fdisk for the disk:
   sudo fdisk /dev/sda  
  1. Delete the partition (d) and recreate it (n) with the new size.
  2. Note: Resizing may require additional tools like resize2fs for ext4 partitions.

Verify Partition Changes

  1. Start fdisk for the disk:
   sudo fdisk /dev/sda  
  1. Press v to verify the partition table for errors.

Save Changes and Exit

  1. Start fdisk for the disk:
   sudo fdisk /dev/sda  
  1. Press w to write changes and exit.

Discard Changes and Exit

  1. Start fdisk for the disk:
   sudo fdisk /dev/sda  
  1. Press q to quit without saving changes.

Create a GPT Partition Table

  1. Start fdisk for the disk:
   sudo fdisk /dev/sda  
  1. Press g to create a new GPT partition table.
  2. Press w to save changes.

Create an MBR Partition Table

  1. Start fdisk for the disk:
   sudo fdisk /dev/sda  
  1. Press o to create a new MBR (DOS) partition table.
  2. Press w to save changes.

Display Help Menu

  1. Start fdisk for the disk:
   sudo fdisk /dev/sda  
  1. Press m to display the help menu with all available commands.

Key Notes:

  • Backup Data: Always back up your data before modifying partitions.
  • Partition Types: Use l in fdisk to list all supported partition types.
  • GPT vs. MBR: GPT supports larger disks (>2TB) and more partitions.
Share This Article