fdisk - Linux
Overview
The fdisk
command in Linux is a versatile disk partitioning tool primarily used to create, delete, resize, and manage disk partitions on a hard drive. It works with various filesystem types and is most effective in storage management, system setup, and recovery actions.
Syntax
The standard syntax for using fdisk
is as follows:
fdisk [options] <disk>
<disk>
refers to the specific block device (e.g.,/dev/sda
).
Key Syntax Variants:
- To list all partitions:
fdisk -l
- To interactively edit a disk:
fdisk /dev/sda
Options/Flags
Here is a breakdown of some common options used with fdisk
:
-l
: List the partition tables for the specified devices and then exit. If no device is specified, partitions for all devices are shown.t
: Change partition type to match specific filesystems or use cases.d
: Delete a partition. This option does not delete data persistently but removes the entry from the partition table.n
: Add a new partition. The user can specify the type, start, and end sectors.w
: Write the changes made to the disk and exit. It’s essential to execute this command to save any changes.q
: Quit without saving changes.
Examples
Basic Partition Listing
fdisk -l /dev/sda
This command lists all partitions on the /dev/sda
device.
Creating a New Partition
sudo fdisk /dev/sda
After entering the interactive mode:
- Press
n
to create a new partition. - Choose primary (
p
) or extended (e
). - Select the partition number.
- Set the start and end sectors.
- Press
w
to write the changes.
Deleting a Partition
sudo fdisk /dev/sda
- Enter
d
. - Choose the partition number to delete.
- Press
w
to save changes and exit.
Common Issues
Problem: Failing to save changes – Users often reboot and find no changes took effect.
Solution: Ensure to press w
after making changes in the fdisk
utility to write changes to disk.
Problem: Incorrectly identifying disk devices.
Solution: Always double-check the device name with fdisk -l
before applying changes to avoid data loss.
Integration
fdisk
can be combined with filesystem tools to prepare and format new partitions. For example, after creating a partition with fdisk
, you can format it with mkfs.ext4
:
sudo mkfs.ext4 /dev/sda1
Related Commands
- parted, gparted: Alternative partitioning tools offering more features and flexibility.
- mkfs: Used for creating file systems on partitions made with
fdisk
. - lsblk: Lists all block devices, a useful check before and after using
fdisk
.
For further reading and advanced options, consult the official Linux man page for fdisk
or the documentation of your Linux distribution.