Linux parted Command

Linux 命令大全Linux Command Collection


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

parted [Options] [Device] [Command[Parameters]]

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

sudo parted /dev/sdX

will/dev/sdXReplace with your actual device name (e.g./dev/sda)

Common Commands in Interactive Mode

View help

Example

help [command name]

View partition table

Example

print

Switch units

Example

unit GB # Switch to GB display
unit MB # Switch to MB display

Create a new partition

Example

mkpart [partition type] [file system type] start position end position

Example:

Example

mkpart primary ext4 1GB 10GB

Delete a partition

Example

rm [partition number]

Resize a partition

Example

resizepart [partition number] end position

Set partition flag

Example

set [partition number] [flag] on/off

Common flags:boot, lvm, raid, swapwait

Exit

Example

quit

4. Common Operation Examples

Example 1: View All Disk Partitions

Example

sudo parted -l

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

sudo parted /dev/sdb mklabel gpt

Example 3: Create a Primary Partition (Non-interactive)

Example

sudo parted -s /dev/sdb mkpart primary ext4 1MiB 10GiB

Example 4: Resize a Partition

Example

sudo parted -s /dev/sdb resizepart 2 20GiB

Example 5: Set the Boot Flag

Example

sudo parted -s /dev/sda set 1 boot on

5. Advanced Features and Precautions

1. Partition Alignment Optimization

Modern disks (especially SSDs) require proper partition alignment for optimal performance:

Example

sudo parted -a optimal /dev/sdb mkpart primary ext4 0% 100%

2. Convert Partition Table Type

Example

sudo parted /dev/sdb mklabel msdos  # Convert to MBR
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

sudo mkfs.ext4 /dev/sdb1

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 命令大全Linux Command Collection

Other extensions