Overview
fdisk is a powerful tool that allows users to directly manipulate a disk's partition table. It is used to divide a disk into multiple logical sections for installing multiple operating systems or partitioning disk space for specific purposes. fdisk is optimized for MBR (Master Boot Record) style partition tables, and for GPT (GUID Partition Table) style disks, it is safer and more convenient to use dedicated tools like 'gdisk'.
Key Features
The main features of the fdisk command are as follows:
- Supports command-line based interactive mode.
- Primarily manages MBR (Master Boot Record) partition tables.
- Provides various functions such as creating, deleting, and changing partition types.
- Changes are not actually applied to the disk until saved, ensuring safety.
Differences between fdisk and parted
fdisk and parted are both partition management tools, but they differ in the partition table types they support.
- fdisk: Specialized for MBR partition tables. May not handle disks larger than 2TB correctly.
- parted: Supports both MBR and GPT partition tables. Suitable for managing large disks over 2TB.
Key Options
fdisk primarily operates by entering an interactive mode for a specific device, rather than using options with `--`.
1) Execution Options
Generated command:
Try combining the commands.
Description:
`fdisk` Executes the command.
Combine the above options to virtually execute commands with AI.
Interactive Mode Internal Commands
When fdisk is executed for a specific device (e.g., `sudo fdisk /dev/sda`), it enters interactive mode. These are the main commands available in this mode.
Key Internal Commands
In interactive mode, typing `m` displays the full list of commands.
- m: Prints the command list.
- p: Prints the current disk's partition table.
- n: Creates a new partition.
- d: Deletes an existing partition.
- t: Changes the partition's type.
- w: Writes changes to disk and exits. **This command is irreversible.**
- q: Exits without saving changes.
Usage Examples
Learn the functions of the fdisk command through various usage examples.
Output Full Partition Information
sudo fdisk -l
Checks partition information for all disks connected to the system. Useful for understanding disk status before partition operations.
View Specific Disk Partition Table
sudo fdisk /dev/sdb
(Enter 'p' in interactive mode)
(Enter 'q' in interactive mode)
Execute `fdisk` on the `/dev/sdb` disk, then enter the `p` command in interactive mode to view the partition table. Press `q` to exit without saving.
Create a New Partition
sudo fdisk /dev/sdb
(Enter n, p, 1, Enter, Enter, w)
An example of the process for creating a partition on a new disk. Enter `n` and follow the default settings. Finally, you must enter `w` for the changes to be saved.
Installation
fdisk is part of the `util-linux` package and is included by default in most Linux distributions. No separate installation is required.
Tips & Cautions
Here are some points to note when using the fdisk command.
Tips
- fdisk is a very powerful tool, so it should always be run with **`sudo` privileges**. Performing operations on the wrong disk can lead to data loss.
- Always verify that the target disk is correct with the `fdisk -l` command before performing operations.
- After creating a partition, you must create a file system using the `mkfs` command to make it usable.
- Changes are not applied until the `w` command is executed in interactive mode, so even if you make a mistake, you can safely exit with the `q` command.