Linux parted Command
1. Overview of the parted Command
parted is a powerful disk partitioning tool in Linux systems. It supports multiple partition table types (such as GPT and MBR), and can create, delete, resize, and manage disk partitions. Compared with the traditional fdisk, parted is especially suitable for handling large-capacity disks (over 2TB) and modern partitioning needs.
Main Features
- Interactive and non-interactive: Supports both direct command-line operations and interactive operations.
- Immediate effect: Most operations are written to disk immediately without extra confirmation.
- Support for multiple file systems: ext2/3/4, xfs, btrfs, fat, ntfs, etc.
- Lossless resizing: Can resize partitions without data loss (requires file system support).
2. Installation and Basic Syntax
Install parted
Most Linux distributions come with parted preinstalled. If you need to install it:
# Debian/Ubuntu sudo apt install parted # CentOS/RHEL sudo yum install parted
Basic Command Syntax
Example
Common Options
| Option | Description |
|---|---|
-l |
List partition information for all block devices |
-s |
Script mode (non-interactive) |
-a |
Set alignment type (min/opt/none) |
-f |
Suppress some warning messages |
3. Interactive Mode
Entering Interactive Mode
Example
will/dev/sdXReplace with your actual device name (e.g./dev/sda)
Common Commands in Interactive Mode
View help
Example
View partition table
Example
Switch units
Example
unit MB # Switch to MB display
Create a new partition
Example
Example:
Example
Delete a partition
Example
Resize a partition
Example
Set partition flag
Example
Common flags:boot, lvm, raid, swapwait
Exit
Example
4. Common Operation Examples
Example 1: View All Disk Partitions
Example
Example output:
Model: ATA ST1000DM010-2EP1 (scsi) Disk /dev/sda: 1000GB Sector size (logical/physical): 512B/4096B Partition Table: gpt Disk Flags: Number Start End Size File system Name Flags 1 1049kB 538MB 537MB fat32 boot, esp 2 538MB 1000GB 999GB ext4
Example 2: Create a New Partition Table (GPT Format)
Example
Example 3: Create a Primary Partition (Non-interactive)
Example
Example 4: Resize a Partition
Example
Example 5: Set the Boot Flag
Example
5. Advanced Features and Precautions
1. Partition Alignment Optimization
Modern disks (especially SSDs) require proper partition alignment for optimal performance:
Example
2. Convert Partition Table Type
Example
sudo parted /dev/sdb mklabel gpt # Convert to GPT
⚠️ Note: This will delete all partitions on the disk!
3. File System Operations
parted only manages partitions; creating a file system requires additional commands:
Example
4. Precautions
- Data safety: Partition operations may cause data loss; be sure to back up in advance.
- Partition in use: Cannot modify mounted partitions.
- Size units: Explicitly specify units (e.g., GB/MB), otherwise bytes are used by default.
- GPT vs MBR: Disks over 2TB must use the GPT partition table.
6. Comparison of parted with fdisk/gdisk
| Feature | parted | fdisk | gdisk |
|---|---|---|---|
| Interactive mode | Supported | Supported | Supported |
| Script mode | Supported | Not supported | Not supported |
| GPT support | Yes | Limited support | Yes |
| Large disk support | Yes | No (MBR only) | Yes |
| Immediate write | Yes | No (requires the w command) | No (requires the w command) |
| Resize partitions | Yes | no | no |
Linux Command Collection