The fdisk
command in Linux is used to create, delete, modify, and manage disk partitions. It is one of the most commonly used tools for partitioning storage devices.
Summary of fdisk
Commands
Command | Description |
---|---|
fdisk -l |
List partitions |
fdisk /dev/sdb |
Open partition manager for /dev/sdb |
p |
Print partition table |
n |
Create a new partition |
d |
Delete a partition |
t |
Change partition type |
w |
Write changes and exit |
q |
Quit without saving |
The fdisk
command is powerful for managing disk partitions. However, be cautious while modifying partitions to avoid data loss.
1. Display Partition Table (-l
option)
To list all available disks and their partitions:
Example Output:
๐น Explanation:
Device
column shows the partitions (/dev/sda1
,/dev/sda2
).Boot
column (*
) marks the bootable partition.Size
column shows partition sizes.
2. Enter fdisk
Interactive Mode
To modify partitions, open fdisk
for a specific disk:
๐น This opens an interactive mode where you can create, delete, and modify partitions.
3. View Current Partition Table
Inside fdisk
, press:
๐น Displays partition details of /dev/sdb
.
4. Create a New Partition
Step 1: Enter fdisk
Step 2: Create a New Partition
Press n
โ Enter (for new partition).
๐น It will ask for the partition type:
- Press
p
for Primary partition. - Press
e
for Extended partition.
Then, specify the partition number, starting sector, and size (e.g., +10G
for a 10GB partition).
Step 3: Save and Exit
Press:
๐น This writes the changes and exits fdisk
.
5. Delete a Partition
Inside fdisk
:
- Press
d
โ Enter (Deletes a partition). - Enter the partition number to delete.
- Press
w
to save and exit.
6. Change Partition Type
Inside fdisk
:
- Press
t
โ Enter (Change partition type). - Enter the partition number.
- Enter the type code (e.g.,
82
for Swap,83
for Linux). - Press
w
to save changes.
7. Format the New Partition
After creating a partition, format it before use:
๐น Formats /dev/sdb1
with ext4
filesystem.
For Swap Partition:
8. Mount the New Partition
To use the new partition, mount it:
To make it permanent, add to /etc/fstab
:
9. Resize a Partition (Requires resize2fs
)
If you extend a partition, resize the filesystem:
๐น This resizes the filesystem to match the new partition size.