|

Linux CLI 28 🐧 fdisk command

28 – fdisk command

sudo fdisk -l
sudo fdisk -l /dev/sda
sudo fdisk -s /dev/sda
sudo fdisk /dev/sda #then m

fdisk is a command-line tool for managing partition tables on HDDs, SSDs, and other storage devices. You can create, delete, resize partitions, change their types, or check their status.

Key point: fdisk edits the partition table, not the filesystem. Always back up data before modifying partitions.


a – fdisk

fdisk is used to manage partition tables on HDD/SSD/devices.

You can:

  • Create partitions
  • Delete partitions
  • Resize partitions
  • Change their types
  • Check their status
CommandDescription
fdisk -lLists the partition tables of your devices
fdisk -l /dev/sdaLists the partition tables for the sda device
fdisk -s /dev/sdaPrints the size of the sda partition
fdisk /dev/sdamShows all the disk commands

Examples:

# List all devices and their partitions
$ sudo fdisk -l
Disk /dev/sda: 238.47 GiB, 256060514304 bytes, 500118192 sectors
Disk model: Samsung SSD 860
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: gpt
Disk identifier: 12345678-ABCD-EF00-1234-56789ABCDEF0

Device       Start       End   Sectors   Size Type
/dev/sda1     2048   1050623   1048576   512M EFI System
/dev/sda2  1050624 500118158 499067535 237.9G Linux filesystem

Disk /dev/sdb: 931.51 GiB, 1000204886016 bytes, 1953525168 sectors
Disk model: WDC WD10EZEX
Units: sectors of 1 * 512 = 512 bytes
Sector size (logical/physical): 512 bytes / 4096 bytes
Disklabel type: dos
Disk identifier: 0xabcd1234

Device     Boot Start        End    Sectors   Size Id Type
/dev/sdb1        2048 1953521663 1953519616 931.5G 83 Linux

Disk /dev/sdc: 14.9 GiB, 16008609792 bytes, 31266816 sectors
Disk model: USB Flash Drive
Units: sectors of 1 * 512 = 512 bytes
Disklabel type: dos
Disk identifier: 0x5678efgh

Device     Boot Start      End  Sectors  Size Id Type
/dev/sdc1        2048 31266815 31264768 14.9G  c W95 FAT32 (LBA)

# List partitions for a specific device
$ sudo fdisk -l /dev/sda
Disk /dev/sda: 238.47 GiB, 256060514304 bytes, 500118192 sectors
Disk model: Samsung SSD 860
Units: sectors of 1 * 512 = 512 bytes
Disklabel type: gpt
Disk identifier: 12345678-ABCD-EF00-1234-56789ABCDEF0

Device       Start       End   Sectors   Size Type
/dev/sda1     2048   1050623   1048576   512M EFI System
/dev/sda2  1050624 500118158 499067535 237.9G Linux filesystem

# Print the size of a partition
$ sudo fdisk -s /dev/sda
500118192

$ sudo fdisk -s /dev/sda1
1048576

# Open the interactive fdisk menu
$ sudo fdisk /dev/sda

Welcome to fdisk (util-linux 2.37.2).
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)

  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

b – create, delete and change partition type with fdisk

Create a partition:

$ sudo fdisk /dev/sda
Command (m for help): n          # new partition
Partition type
   p   primary (1 primary, 0 extended, 3 free)
   e   extended (container for logical partitions)
Select (default p): p            # primary
Partition number (2-4, default 2): 2
First sector (1050624-500118158, default 1050624): 1050624
Last sector, +/-sectors or +/-size{K,M,G,T,P} (1050624-500118158, default 500118158): +50G

Created a new partition 2 of type 'Linux filesystem' and of size 50 GiB.

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

Delete a partition:

$ sudo fdisk /dev/sda
Command (m for help): d          # delete partition
Partition number (1,2, default 2): 2

Partition 2 has been deleted.

Command (m for help): w          # write changes
The partition table has been altered.

Change partition type:

$ sudo fdisk /dev/sda
Command (m for help): t          # change type
Partition number (1,2, default 2): 2
Hex code or alias (type L to list all): L    # list types

 0  Empty           24  NEC DOS         81  Minix / old Lin  bf  Solaris
 1  FAT12           27  Hidden NTFS Win 82  Linux swap / So  c1  DRDOS/sec (FAT-
 2  XENIX root      39  Plan 9          83  Linux            c4  DRDOS/sec (FAT-
 3  XENIX usr       3c  PartitionMagic  84  OS/2 hidden or   c6  DRDOS/sec (FAT-
 4  FAT16 <32M      40  Venix 80286     85  Linux extended   c7  Syrinx
 5  Extended        41  PPC PReP Boot   86  NTFS volume set  da  Non-FS data
 6  FAT16           42  SFS             87  NTFS volume set  db  CP/M / CTOS / .
 7  HPFS/NTFS/exFAT 4d  QNX4.x          88  Linux plaintext  de  Dell Utility
 8  AIX             4e  QNX4.x 2nd part 8e  Linux LVM        df  BootIt
 9  AIX bootable    4f  QNX4.x 3rd part 93  Amoeba           e1  DOS access
 a  OS/2 Boot Manag 50  OnTrack DM      94  Amoeba BBT       e3  DOS R/O
 b  W95 FAT32       51  OnTrack DM6 Aux 9f  BSD/OS           e4  SpeedStor
 c  W95 FAT32 (LBA) 52  CP/M            a0  IBM Thinkpad hi  eb  BeOS fs
 e  W95 FAT16 (LBA) 53  OnTrack DM6 Aux a5  FreeBSD          ee  GPT
 f  W95 Ext'd (LBA) 54  OnTrackDM6      a6  OpenBSD          ef  EFI (FAT-12/16/
10  OPUS            55  EZ-Drive        a7  NeXTSTEP         f0  Linux/PA-RISC b
11  Hidden FAT12    56  Golden Bow      a8  Darwin UFS       f1  SpeedStor
12  Compaq diagnost 5c  Priam Edisk     a9  NetBSD           f4  SpeedStor
14  Hidden FAT16 <3 61  SpeedStor       ab  Darwin boot      f2  DOS secondary
16  Hidden FAT16    63  GNU HURD or Sys af  HFS / HFS+       fb  VMware VMFS
17  Hidden HPFS/NTF 64  Novell Netware  b7  BSDI fs          fc  VMware VMKCORE
18  AST SmartSleep  65  Novell Netware  b8  BSDI swap        fd  Linux raid auto
1b  Hidden W95 FAT3 70  DiskSecure Mult bb  Boot Wizard hid  fe  LANstep
1c  Hidden W95 FAT3 75  PC/IX           be  Solaris boot     ff  BBT
1e  Hidden W95 FAT1 80  Old Minix

Hex code or alias (type L to list all): 83

Changed type of partition 'Linux filesystem' to 'Linux'.

Command (m for help): w          # write changes
The partition table has been altered.

Quit without saving:

$ sudo fdisk /dev/sda
Command (m for help): q          # quit — no changes saved

Create a new empty partition table:

KeyPartition Table Type
bBSD disklabel
oMBR (DOS)
gGPT
GSGI (IRIX)
sSun
$ sudo fdisk /dev/sda
Command (m for help): g          # create new empty GPT partition table
Created a new GPT disklabel (GUID: ...).

Command (m for help): w

Complete Example Session

# ============================================
# PART 1: LIST DEVICES AND PARTITIONS
# ============================================

$ sudo fdisk -l
Disk /dev/sda: 238.47 GiB, 256060514304 bytes, 500118192 sectors
Disk model: Samsung SSD 860
Disklabel type: gpt
Disk identifier: 12345678-ABCD-EF00-1234-56789ABCDEF0

Device       Start       End   Sectors   Size Type
/dev/sda1     2048   1050623   1048576   512M EFI System
/dev/sda2  1050624 500118158 499067535 237.9G Linux filesystem

Disk /dev/sdc: 14.9 GiB, 16008609792 bytes, 31266816 sectors
Disk model: USB Flash Drive
Disklabel type: dos
Disk identifier: 0x5678efgh

Device     Boot Start      End  Sectors  Size Id Type
/dev/sdc1        2048 31266815 31264768 14.9G  c W95 FAT32 (LBA)

# ============================================
# PART 2: SPECIFIC DEVICE
# ============================================

$ sudo fdisk -l /dev/sda
Disk /dev/sda: 238.47 GiB, 256060514304 bytes, 500118192 sectors
Device       Start       End   Sectors   Size Type
/dev/sda1     2048   1050623   1048576   512M EFI System
/dev/sda2  1050624 500118158 499067535 237.9G Linux filesystem

# ============================================
# PART 3: PARTITION SIZE
# ============================================

$ sudo fdisk -s /dev/sda
500118192

$ sudo fdisk -s /dev/sda1
1048576

# ============================================
# PART 4: INTERACTIVE MENU
# ============================================

$ sudo fdisk /dev/sda
Command (m for help): m
# ... help shown ...

Command (m for help): p          # print partition table
Disk /dev/sda: 238.47 GiB, 256060514304 bytes, 500118192 sectors
Device       Start       End   Sectors   Size Type
/dev/sda1     2048   1050623   1048576   512M EFI System
/dev/sda2  1050624 500118158 499067535 237.9G Linux filesystem

Command (m for help): q          # quit without saving

# ============================================
# PART 5: CREATE A NEW PARTITION
# ============================================

$ sudo fdisk /dev/sda
Command (m for help): n
Partition type
   p   primary (2 primary, 0 extended, 2 free)
   e   extended (container for logical partitions)
Select (default p): p
Partition number (3,4, default 3): 3
First sector (499068928-500118158, default 499068928): 499068928
Last sector, +/-sectors or +/-size{K,M,G,T,P} (499068928-500118158, default 500118158): +512M

Created a new partition 3 of type 'Linux filesystem' and of size 512 MiB.

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

# ============================================
# PART 6: DELETE A PARTITION
# ============================================

$ sudo fdisk /dev/sda
Command (m for help): d
Partition number (1-3, default 3): 3

Partition 3 has been deleted.

Command (m for help): w
The partition table has been altered.

# ============================================
# PART 7: CHANGE PARTITION TYPE
# ============================================

$ sudo fdisk /dev/sda
Command (m for help): t
Partition number (1,2, default 2): 1
Hex code or alias (type L to list all): ef

Changed type of partition 'EFI System' to 'EFI (FAT-12/16/32)'.

Command (m for help): w
The partition table has been altered.

# ============================================
# PART 8: CREATE NEW EMPTY GPT TABLE
# ============================================

$ sudo fdisk /dev/sdc
Command (m for help): g
Created a new GPT disklabel (GUID: ...).

Command (m for help): w
The partition table has been altered.

# ============================================
# PART 9: VERIFY CHANGES
# ============================================

$ sudo fdisk -l /dev/sda
Disk /dev/sda: 238.47 GiB, 256060514304 bytes, 500118192 sectors
Device       Start       End   Sectors   Size Type
/dev/sda1     2048   1050623   1048576   512M EFI System
/dev/sda2  1050624 500118158 499067535 237.9G Linux filesystem

Quick Reference

Listing / Info

CommandPurpose
fdisk -lList all devices and partitions
fdisk -l /dev/sdaList partitions of sda
fdisk -s /dev/sdaPrint size of sda partition
fdisk /dev/sdaOpen interactive menu

Interactive Menu — Common Keys

KeyAction
mShow help menu
pPrint partition table
nNew partition
dDelete partition
tChange partition type
lList known partition types
FList free unpartitioned space
vVerify partition table
iPrint info about a partition
wWrite changes and exit
qQuit without saving

Partition Type Keys

KeyAction
pPrimary partition
eExtended partition
LList all partition types (in t mode)

New Empty Partition Table

KeyTable Type
bBSD disklabel
oMBR (DOS)
gGPT
GSGI (IRIX)
sSun

Common Hex Type Codes

CodeType
83Linux filesystem
82Linux swap
8eLinux LVM
fdLinux RAID
efEFI System
bW95 FAT32
cW95 FAT32 (LBA)
7HPFS/NTFS/exFAT

Best Practices

Do This:

# Always back up data before partitioning
rsync -av /home /backup/                # ✅

# Use fdisk -l first to identify devices
sudo fdisk -l                          # ✅

# Print the table before making changes
sudo fdisk /dev/sda → p                # ✅

# Use q to abort if unsure
sudo fdisk /dev/sda → q                # ✅

# Write changes with w only when sure
sudo fdisk /dev/sda → w                # ✅

# Re-read the partition table after changes
sudo partprobe /dev/sda                # ✅

# Create filesystem after partitioning
sudo mkfs.ext4 /dev/sda3               # ✅

Don’t Do This:

# Don't modify partitions on a mounted disk
sudo fdisk /dev/sda                    # ❌ if mounted

# Don't write changes without reviewing
sudo fdisk /dev/sda → w                # ❌ careful!

# Don't confuse device names
sudo fdisk /dev/sda                    # ❌ check first!

# Don't forget to create a filesystem
sudo fdisk /dev/sda → n → w            # ❌ no mkfs yet

# Don't use fdisk on GPT for >2TB without care
# Use parted or gdisk for large disks      # ✅

Common Pitfalls

PitfallProblemSolution
Wrong deviceData lossVerify with fdisk -l
Forgot to writeChanges lostPress w
Used q by mistakeChanges discardedRe-do and press w
Mounted diskKernel won’t re-readUnmount first
No filesystemPartition unusableRun mkfs
Forgot sudoPermission deniedPrefix with sudo
Deleted wrong partitionData lossRestore from backup
Large disk (>2TB)MBR limitUse GPT (g)

Real-World Examples

1. Identify All Disks

$ sudo fdisk -l
Disk /dev/sda: 238.47 GiB ...
Disk /dev/sdb: 931.51 GiB ...
Disk /dev/sdc: 14.9 GiB ...

2. View Partitions of One Disk

$ sudo fdisk -l /dev/sda
Device       Start       End   Sectors   Size Type
/dev/sda1     2048   1050623   1048576   512M EFI System
/dev/sda2  1050624 500118158 499067535 237.9G Linux filesystem

3. Create a New Partition

$ sudo fdisk /dev/sdb
Command (m for help): n
Partition type
   p   primary (1 primary, 0 extended, 3 free)
   e   extended
Select (default p): p
Partition number (2-4, default 2): 2
First sector (1953521664-1953525167, default 1953521664): 1953521664
Last sector, +/-sectors or +/-size{K,M,G,T,P}: +100G

Created a new partition 2 of type 'Linux filesystem' and of size 100 GiB.

Command (m for help): w

4. Delete a Partition

$ sudo fdisk /dev/sdb
Command (m for help): d
Partition number (1,2, default 2): 2

Partition 2 has been deleted.

Command (m for help): w

5. Change a Partition Type to Linux Swap

$ sudo fdisk /dev/sdb
Command (m for help): t
Partition number (1,2, default 2): 2
Hex code or alias (type L to list all): 82

Changed type of partition 'Linux filesystem' to 'Linux swap / Solaris'.

Command (m for help): w

6. Create a New GPT Table (Wipe Disk)

$ sudo fdisk /dev/sdc
Command (m for help): g
Created a new GPT disklabel (GUID: ...).

Command (m for help): w

7. Format the New Partition

# After creating the partition
$ sudo mkfs.ext4 /dev/sdb2

# Mount it
$ sudo mkdir /mnt/newdisk
$ sudo mount /dev/sdb2 /mnt/newdisk

8. Verify with lsblk

$ lsblk
NAME   MAJ:MIN RM   SIZE RO TYPE MOUNTPOINT
sda      8:0    0 238.5G  0 disk
├─sda1   8:1    0   512M  0 part /boot/efi
└─sda2   8:2    0 237.9G  0 part /
sdb      8:16   0 931.5G  0 disk
└─sdb1   8:17   0 931.5G  0 part /home
sdc      8:32   1  14.9G  0 disk
└─sdc1   8:33   1  14.9G  0 part /media/usbA

Visual: Partition Table Editing

┌──────────────────────────────────────────────┐
│           Disk /dev/sda (238 GB)             │
│                                              │
│  ┌──────────────┬──────────────────────────┐ │
│  │  /dev/sda1   │       /dev/sda2          │ │
│  │  512 MB EFI  │   237.9 GB Linux         │ │
│  └──────────────┴──────────────────────────┘ │
│                                              │
└─────────────────┬────────────────────────────┘
                  │
                  │  sudo fdisk /dev/sda
                  │  → n (new partition)
                  │  → p (primary)
                  │  → 3 (number)
                  │  → +50G (size)
                  │  → w (write)
                  ▼
┌──────────────────────────────────────────────┐
│           Disk /dev/sda (238 GB)             │
│                                              │
│  ┌──────────────┬───────────────┬─────────┐  │
│  │  /dev/sda1   │  /dev/sda2    │ sda3    │  │
│  │  512 MB EFI  │ 237.9 GB Linux│ 50 GB   │  │
│  └──────────────┴───────────────┴─────────┘  │
│                                              │
└──────────────────────────────────────────────┘

Summary

CommandPurposeExample
fdisk -lList all devicessudo fdisk -l
fdisk -l /dev/sdaList one devicesudo fdisk -l /dev/sda
fdisk -s /dev/sdaShow sizesudo fdisk -s /dev/sda
fdisk /dev/sdaOpen menusudo fdisk /dev/sda
mHelp menuinside fdisk
pPrint tableinside fdisk
nNew partitioninside fdisk
dDelete partitioninside fdisk
tChange typeinside fdisk
LList typesinside t mode
wWrite & exitinside fdisk
qQuit without savinginside fdisk
gNew GPT tableinside fdisk
oNew MBR tableinside fdisk

Key takeaways:

  • fdisk manages partition tables on HDD/SSD/USB devices
  • Use fdisk -l to list devices and partitions
  • Use fdisk -l /dev/sda to focus on one device
  • Use fdisk -s /dev/sda to print the size
  • Inside fdisk: n = new, d = delete, t = type, p = print, w = write, q = quit
  • Press L in t mode to list all partition type codes
  • Create new tables with g (GPT), o (MBR), G (SGI), s (Sun)
  • Always back up before partitioning — changes can destroy data
  • After partitioning, create a filesystem with mkfs

Remember: fdisk edits the partition table, not the filesystem. Use fdisk -l to identify devices, then fdisk /dev/sdX to edit. Inside, n creates, d deletes, t changes type, w writes, q quits. Always verify the device name first — a wrong keystroke can wipe a disk. After partitioning, run mkfs to format, then mount to use. Master fdisk, and you control how storage is divided on your system.


Stop using slow, ad-bloated tool sites! 🤮

🔎 Search “KandZ Tools” on Google to use many professional utilities for free.

KandZ.me is the ultimate minimalist hub for:
✅ Finance (Mortgage, Interest, Inflation)
✅ Tech (Base64, JSON, Dev Suite, IP)
✅ Health (BMI, BMR, TDEE)
✅ Productivity (Timer, Workspace, QR)

⚡️ Fast & Private
🔒 No data leaves your device
💎 100% Free

🔗 Use it now: https://tools.kandz.me
🔖 Bookmark it—you’ll need it later!