How to use the command 'fdisk' (with examples)
- Linux
- December 25, 2023
The fdisk
command is a program for managing partition tables and partitions on a hard disk. It allows users to create, delete, and view partition tables on a disk.
Use case 1: List partitions
Code:
sudo fdisk -l
Motivation: This use case allows you to list all the partitions on a disk. This can be useful when you want to check the existing partitions on your hard disk or identify specific partitions for further manipulation.
Explanation:
sudo
: This command is used to runfdisk
with root privileges. It allows you to perform partitioning operations on your hard disk.fdisk
: This is the command itself.-l
: This option is used to list all partitions on the disk.
Example output:
Disk /dev/sda: 238.5 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
…
Device Boot Start End Sectors Size Id Type
/dev/sda1 * 2048 514047 512000 250M 7 HPFS/NTFS/exFAT
/dev/sda2 514048 292894975 292380928 139.2G 7 HPFS/NTFS/exFAT
/dev/sda3 508293120 500117503 -8175616 3.9G 82 Linux swap / Solaris
...
Use case 2: Start the partition manipulator
Code:
sudo fdisk /dev/sdX
Motivation: This use case allows you to start the partition manipulator for a specific disk, where ‘sdX’ represents the device path of the disk you want to partition. This can be helpful when you want to create, delete, or modify partitions on a specific disk.
Explanation:
sudo
: This command is used to runfdisk
with root privileges.fdisk
: This is the command itself./dev/sdX
: Replace ‘sdX’ with the device path of the disk you want to partition. For example,/dev/sda
represents the first hard disk in most cases.
Example:
sudo fdisk /dev/sda
Use case 3: Create a partition
Code:
n
Motivation: This use case allows you to create a new partition on a disk. When you need to allocate a specific space on your disk for a new partition, you can use this command.
Explanation:
n
: This command is used within thefdisk
interactive mode to create a new partition.
Example output:
Partition type:
p primary (0 primary, 0 extended, 4 free)
e extended (container for logical partitions)
Select (default p):
In this example, the n
command prompts you to select the partition type, where ‘p’ represents a primary partition and ’e’ represents an extended partition.
Use case 4: Select a partition to delete
Code:
d
Motivation: This use case allows you to delete an existing partition on a disk. When you want to remove an unnecessary partition or merge it with another partition, you can use this command.
Explanation:
d
: This command is used within thefdisk
interactive mode to delete a partition.
Example output:
Partition number (1,2,3,4, default 4): 3
In this example, the d
command prompts you to select the partition number to delete. Here, ‘3’ represents the third partition on the disk.
Use case 5: View the partition table
Code:
p
Motivation: This use case allows you to view the partition table of a disk. It provides information about the existing partitions, including their sizes, types, and starting/ending sectors.
Explanation:
p
: This command is used within thefdisk
interactive mode to view the partition table.
Example output:
Command (m for help): p
Disk /dev/sda: 238.5 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
…
Device Boot Start End Sectors Size Id Type
/dev/sda1 * 2048 514047 512000 250M 7 HPFS/NTFS/exFAT
/dev/sda2 514048 292894975 292380928 139.2G 7 HPFS/NTFS/exFAT
/dev/sda3 508293120 500117503 -8175616 3.9G 82 Linux swap / Solaris
...
In this example, the p
command displays the partition table of the disk, including the device names, boot flags, start and end sectors, size, and partition type.
Use case 6: Write the changes made
Code:
w
Motivation: This use case allows you to write and save the changes made to the partition table of a disk. When you have finished creating, deleting, or modifying partitions, you need to save the changes for them to take effect.
Explanation:
w
: This command is used within thefdisk
interactive mode to write the changes made to the partition table.
Example output:
Command (m for help): w
The partition table has been altered.
Syncing disks.
In this example, the w
command saves the changes made to the partition table and outputs a confirmation message.
Use case 7: Discard the changes made
Code:
q
Motivation: This use case allows you to discard the changes made to the partition table of a disk. When you want to cancel the modifications made during the interactive session, you can use this command.
Explanation:
q
: This command is used within thefdisk
interactive mode to exit without saving any changes.
Example output:
Command (m for help): q
In this example, the q
command cancels the modifications made during the session and exits fdisk
without saving any changes.
Use case 8: Open a help menu
Code:
m
Motivation: This use case allows you to access the help menu within fdisk
to get information about available commands and their usage.
Explanation:
m
: This command is used within thefdisk
interactive mode to open a help menu.
Example output:
m
Command action
a toggle a bootable flag
b edit bsd disklabel
c toggle the dos compatibility flag
d delete a partition
g create a new empty GPT partition table
G create an IRIX (SGI) partition table
l list known partition types
m print this menu
...
In this example, the m
command displays the available commands and their actions within the fdisk
help menu.
Conclusion:
The fdisk
command is a versatile tool for managing partition tables and partitions on a hard disk. With its various options and commands, users can create, delete, and view partitions, as well as write or discard changes made to the partition table. When used correctly, fdisk
provides a powerful means of partition management for Linux systems.