fdisk / parted (Disk Partition Management)
fdisk and parted are Linux commands for managing disk partitions. fdisk lets you create, delete, and modify partitions interactively, and supports both the traditional MBR (Master Boot Record) format and GPT (GUID Partition Table). parted has a non-interactive mode that is well-suited for scripting, and is a good fit for managing large disks over 2 TB and GPT partitions. MBR is limited to a maximum of 4 primary partitions and a disk size of 2 TB, whereas GPT far exceeds both of those limits.
Syntax
# -----------------------------------------------
# fdisk basic syntax
# -----------------------------------------------
# fdisk -l
# → Lists all disks and partitions on the system
# Example: sudo fdisk -l
# fdisk -l {device}
# → Shows partition information for the specified device
# Example: sudo fdisk -l /dev/sdb
# fdisk {device}
# → Starts interactive mode for the specified device
# Example: sudo fdisk /dev/sdb
#
# Interactive mode subcommands:
# p → Print the current partition table
# n → Create a new partition
# d → Delete a partition
# t → Change the partition type
# Examples: 8e = Linux LVM, 82 = Linux swap, 83 = Linux
# g → Create a new GPT partition table
# o → Create a new MBR (DOS) partition table
# w → Write changes to disk and exit
# q → Quit without saving changes
# m → Display help
# -----------------------------------------------
# parted basic syntax
# -----------------------------------------------
# parted {device}
# → Starts interactive mode for the specified device
# Example: sudo parted /dev/sdb
#
# Interactive mode subcommands:
# print → Print the current partition table
# mklabel {type} → Create a new partition table (gpt / msdos)
# mkpart {type} {filesystem} {start} {end}
# → Create a partition
# Types: primary / logical / extended
# Example: mkpart primary ext4 1MiB 50GiB
# rm {number} → Delete the partition with the given number
# resizepart {number} {new-end}
# → Change the end position of a partition
# quit → Exit interactive mode
# parted {device} {subcommand}
# → Run a single command in non-interactive mode (suitable for scripts)
# Example: sudo parted /dev/sdb print
# Example: sudo parted /dev/sdb mklabel gpt
Command Reference
| Command / Operation | Description |
|---|---|
fdisk -l | Lists all disks and partitions on the system. Useful for identifying disk names. |
fdisk -l /dev/sdb | Shows partition information for the specified device only. |
fdisk /dev/sdb | Starts interactive mode. Use n to create, d to delete, and w to write changes. |
n (fdisk interactive) | Creates a new partition. You specify the type (primary or logical), starting sector, and ending sector interactively. |
d (fdisk interactive) | Deletes a partition. You select the target by entering its partition number. |
p (fdisk interactive) | Prints the current partition table. Useful for checking the state before and after making changes. |
t (fdisk interactive) | Changes the partition type. Use 8e for LVM, 82 for swap, and 83 for a standard Linux partition. |
g (fdisk interactive) | Creates a new GPT partition table. All existing partition data will be erased. |
w (fdisk interactive) | Writes changes to disk and exits. Changes are not applied until you run w. |
q (fdisk interactive) | Quits without saving changes. Use this to cancel accidental operations. |
parted /dev/sdb print | Prints the partition table for the specified device. Works with both GPT and MBR. |
parted /dev/sdb mklabel gpt | Creates a GPT partition table. Suitable for disks larger than 2 TB and UEFI environments. |
parted /dev/sdb mklabel msdos | Creates an MBR (DOS) partition table. Use this when compatibility with legacy BIOS environments is required. |
parted /dev/sdb mkpart primary ext4 1MiB 50GiB | Creates a primary partition starting at 1MiB and ending at 50GiB. |
parted /dev/sdb rm 1 | Deletes partition number 1. |
parted /dev/sdb resizepart 1 100GiB | Changes the end position of partition number 1 to 100GiB. |
lsblk | Lists block devices in a tree format. Convenient for checking partitions. |
lsblk -f | Also shows the filesystem type, UUID, and mount point for each device. |
Examples
Creating partitions with fdisk
# ----------------------------------------------- # Create two partitions on a new disk /dev/sdb # ----------------------------------------------- # Check the current state first sudo fdisk -l /dev/sdb # Start fdisk interactive mode sudo fdisk /dev/sdb # --- The following are operations inside interactive mode --- # p: Print the current partition table (empty initially) # g: Create a new GPT partition table # (Use o instead if you need MBR) # n: Create the first partition # Partition number: 1 (default) # First sector: (press Enter to use default) # Last sector: +50G (allocates 50 GB) # n: Create the second partition # Partition number: 2 (default) # First sector: (press Enter to use default) # Last sector: (press Enter to use default → uses all remaining space) # t: Change the type of the second partition to LVM # Partition number: 2 # Partition type: 31 (Linux LVM) # p: Verify the changes # w: Write changes to disk and exit # Notify the kernel of the new partition layout sudo partprobe /dev/sdb # Verify the result lsblk /dev/sdb
Run the following command:
$ lsblk /dev/sdb NAME MAJ:MIN RM SIZE RO TYPE MOUNTPOINTS sdb 8:16 0 200G 0 disk ├─sdb1 8:17 0 50G 0 part └─sdb2 8:18 0 150G 0 part
Creating GPT partitions with parted
# ----------------------------------------------- # Create GPT partitions on /dev/sdc using parted # ----------------------------------------------- # Check the current state of the disk sudo parted /dev/sdc print # Create a GPT partition table # (All existing data will be erased — proceed with caution) sudo parted /dev/sdc mklabel gpt # First partition: leave 1MiB at the start, then allocate up to 100GiB # (Leaving 1MiB at the start avoids alignment issues) sudo parted /dev/sdc mkpart primary ext4 1MiB 100GiB # Second partition: from 100GiB to the end of the disk sudo parted /dev/sdc mkpart primary xfs 100GiB 100% # Verify the partition table sudo parted /dev/sdc print
Run the following command:
$ sudo parted /dev/sdc print Model: ATA QEMU HARDDISK (scsi) Disk /dev/sdc: 500GB Sector size (logical/physical): 512B/512B Partition Table: gpt Disk Flags: Number Start End Size File system Name Flags 1 1049kB 107GB 107GB ext4 primary 2 107GB 500GB 393GB xfs primary
Resizing a partition and verifying
# ----------------------------------------------- # Expand partition 1 on /dev/sdc to 200GiB # ----------------------------------------------- # Use resizepart to move the end position of partition 1 to 200GiB sudo parted /dev/sdc resizepart 1 200GiB # Verify the state after the change sudo parted /dev/sdc print # Also expand the filesystem (for ext4) # resize2fs can be run while the filesystem is mounted (ext4 online resize) sudo resize2fs /dev/sdc1 # Verify including filesystem type, UUID, and mount point lsblk -f /dev/sdc
Running these commands produces the following output:
$ lsblk -f /dev/sdc NAME FSTYPE FSVER LABEL UUID FSAVAIL FSUSE% MOUNTPOINTS sdc ├─sdc1 ext4 1.0 a1b2c3d4-e5f6-7890-abcd-ef1234567890 185.3G 3% /data/kurisu └─sdc2 xfs data f0e1d2c3-b4a5-6789-0fed-cba987654321
Overview
fdisk and parted are the primary tools for managing disk partition tables on Linux. fdisk is intuitive and has a low learning curve, making it well-suited for routine administration tasks and quick inspections. parted, on the other hand, excels at handling large disks over 2 TB, working with GPT, and automating tasks via scripts. Keep in mind that creating a partition does not create a filesystem — you need to format it separately with mkfs. If you need to manage multiple partitions together with more flexibility, consider using LVM (Logical Volume Manager). When adding or replacing disks, always confirm the device name with lsblk or fdisk -l before proceeding to avoid operating on the wrong device.
If you find any errors or copyright issues, please contact us.