parted Command in Linux with Examples

Last updated: October 24, 2023 | Bobbin Zachariah

The parted is a free GNU utility used to manage hard disk partitions from the command line. It can create, resize, print, and remove partitions on a disk in a Linux system. More often parted tool for disk partitioning for running multiple OS, allocating specific system space, or separating valuable files or extending volumes.

When you run parted command without any options it will print parted package version, choose the first drive by default and wait at the prompt for extra commands. The command has to be run as root or a user with sudo access.

$ sudo parted
GNU Parted 3.2
Using /dev/xvda
Welcome to GNU Parted! Type 'help' to view a list of commands.

Type quit to exit from the parted prompt.

Important: All changes are made to the disk as soon typed your command.

Key Features of parted are:

  • Supports disks over 2TB (GPT).
  • Compatible with GPT, MBR, and BSD disklabels.
  • List, Create, and Delete Partitions.
  • Enables non-destructive partition resizing.
  • Manages specialized partition flags.
  • Comprehensive command-line interface.
  • Includes a rescue mode for partition recovery.

When using parted, it's safest to practice on a non-critical system or virtual machine. In a live environment ensure you choose correct disk and partition numbers before executing commands, as mistakes can be destructive.

Install Parted

Parted is by default installed on most modern Linux distributions. If it's not included in your distribution, install parted manually.

On Ubuntu and Debian flavored distros:

sudo apt install parted

On CentOS Stream and RHEL:

sudo yum install parted

How to use Command Parted

Let's learn some of the most common use of command parted in Linux

1. List All Partitions on Selected Disk using print all

Let's check how to print all disk information and its partitions. By default parted choose first drive. To print all disk partitions type print all.

Note: The warning showing in output is because that disk has unallocated disk space yet to be provisioned.

$ sudo parted
GNU Parted 3.2
Using /dev/xvda
Welcome to GNU Parted! Type 'help' to view a list of commands.
                                                                       (parted) print all
Model: Xen Virtual Block Device (xvd)
Disk /dev/xvda: 8590MB
Sector size (logical/physical): 512B/512B
Partition Table: msdos
Disk Flags:

Number  Start   End     Size    Type     File system  Flags
 1      1049kB  8590MB  8589MB  primary  ext4         boot

Warning: Not all of the space available to /dev/xvdb appears to be
used, you can fix the GPT to use all of the space (an extra 25165824
blocks) or continue with the current setting?
                                                                                                                                              Fix/Ignore? Ignore
Model: Xen Virtual Block Device (xvd)
Disk /dev/xvdb: 21.5GB
Sector size (logical/physical): 512B/512B
Partition Table: gpt
Disk Flags:

Number  Start   End     Size    File system  Name     Flags
 1      17.4kB  1024MB  1024MB  ext4         primary
 2      1024MB  2048MB  1023MB  ext4         primary

Instead, you can also use a single command to print all partitions on the selected disk:

sudo parted /dev/xvda print all

If you have multiple disks, you can select between the disk using the select command:

select the disk

2. Create Logical or Primary partition using mkpart

Normally an operating system is installed on the first disk /dev/sda. When you add a new disk OS will pick the next number as /dev/sdb. To illustrate I am using a Xen virtual disk which uses the naming convention as /dev/xvd.

I have added a new 20GB disk and you can see it as unrecognised disk label.

Display disk

The first step would be to set your required disk label, supported disk label are bsd, loop, gpt, mac, msdos, pc98, and sun.

(parted) mklabel msdos

Now I am going to segment /dev/xvdb into two primary partitions with the first partition with 10GB and second partition with 5GB.

First create a new partition as primary, use mkpart command with start 0 and end 10000:

create a new partition using mkpart

If you want you can create more partitions, run mkpart command again specifying the start and end size

create second partition

Note: The concept of 'primary' reflects from MBR, GPT doesn't care but still have to add a name.

You can also use mkpart to create the partition to span the entire drive by specifying the percentage to use (here 0% to 100%).

For example:

sudo parted -a opt /dev/sda mkpart primary ext4 0% 100%

Instead of using print command, you can run alternative commands such as lsblk, fdisk -l to see partitions created.

$ lsblk
loop0     7:0    0   18M  1 loop /snap/amazon-ssm-agent/1566
loop1     7:1    0 93.8M  1 loop /snap/core/8935
loop2     7:2    0 93.9M  1 loop /snap/core/9066
xvda    202:0    0    8G  0 disk
└─xvda1 202:1    0    8G  0 part /
xvdb    202:16   0   20G  0 disk
├─xvdb1 202:17   0  9.3G  0 part
└─xvdb2 202:18   0  4.7G  0 part

Now we can format the partition (/dev/xvdb1) with ex4 filesystem, use mkfs.ext4 as follows:

sudo mkfs.ext4 /dev/xvdb1

3. Find unallocated space on the disk using print free

To find the unallocated space, choose your disk then run print free command.

find unallocated disk space

We will discuss how to use resizepart command to extend partition when you have more unallocated disk space.

4. Resize disk partition using resizepart

To grow the partition, it has to be resized first. Resize simple means moving the end position of a partition.

Here I am going to resize the second partition of /dev/xvdb, moving end position to 20000:

(parted) resizepart

Note: growpart is another handy tool available on Linux to extend a partition.

To resize each file system to the new capacity, you have to run file system-specific command. To extend filesystem in Linux use resize2fs command as follows:

$ sudo resize2fs /dev/xvdb2
resize2fs 1.44.1 (24-Mar-2018)
Resizing the filesystem on /dev/xvdb2 to 2441340 (4k) blocks.
The filesystem on /dev/xvdb2 is now 2441340 (4k) blocks long.

5. Delete partition from a chosen disk

To delete a partition you should know the partition number on the disk. Use print command in parted to show all partition and its corresponding number.

To delete you can use rm command followed by partition number. Here we going to delete 2nd partition as shown below:

(parted) rm 2
delete partition

6. Set flags on partitions using set

Parted allows to set flags on partition. Don't be surprised some flags depend on disk labels. A flag can be either on or off. Most commons flags are boot, lab, swap, raid, LVM, etc.

The following command set LVM flag on partition 2:

(parted) set 2 LVM on

Another practical example when we need to set a boot partition:

(parted) set 2 boot on

7. Rescue Linux Disk Partition using rescue

Rescue comes to help when you accidentally delete a partition. A lost partition can be recovered by locating between start and end.

Let's delete partition 1 on /dev/xvdb and use rescue command to recover it:

(parted) rescue

8. Set default unit

Unit command in parted helps to set a default unit to display capacities and locations.
unit supported are:

KiB- kibibyte 
MiB - mebibyte
GiB - gibibyte
TiB - tebibyte
kB - kilobyte
MB - megabyte
GB - gigabyte
TB - terabyte
% - percentage of the device
cyl - cylinders
chs - cylinders, heads, sectors addressing
compact - Use human-readable representation for output

The following command set unit to compact:

(parted) unit compact
(parted) print
Model: Xen Virtual Block Device (xvd)
Disk /dev/xvda: 8590MB
Sector size (logical/physical): 512B/512B
Partition Table: msdos
Disk Flags:

Number  Start   End     Size    Type     File system  Flags
 1      1049kB  8590MB  8589MB  primary  ext4         boot

You can also print in units as follows:

(parted) unit GB print
Model: Xen Virtual Block Device (xvd)
Disk /dev/xvda: 8.59GB
Sector size (logical/physical): 512B/512B
Partition Table: msdos
Disk Flags:

Number  Start   End     Size    Type     File system  Flags
 1      0.00GB  8.59GB  8.59GB  primary  ext4         boot



Please add comments below to provide the author your ideas, appreciation and feedback.

Leave a Reply

Leave a Comment