Parted is a free GNU utility used to manage hard disk partitions from the command line. It can create, delete, resize, and print disk partition, and more on Linux.
More often we use parted tool for disk partitioning for running multiple OS, allocating specific system space, or separating valuable files or extending volumes.
Traditionally many users use fdisk tool for partitioning, the primary reason to use parted when disk size bigger than 2TB. Initially parted only supported GPT, and from util-linux 2.23 fdisk also started supporting GPT.
In this tutorial, I will show how to use parted command for disk management in Linux
Install Parted on Linux
Parted is by default installed on most modern Linux distributions. If it's not included in your distribution, install parted manually.
Installing parted on Ubuntu and Debian flavored distros:
$ sudo apt-get install parted
Installing parted on CentOS and RHEL:
$ sudo yum install parted
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. Parted 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. (parted)
Type quit
to exit from the parted prompt.
Important: All changes are made to the disk as soon typed your command.
List Disk Partitions
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.
ubuntu@linoxide:~$ 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 (parted)
Instead, you can also use a single command:
$ sudo parted /dev/xvda print all
If you have multiple disks, you can select between the disk using the select command:

Find unallocated space on the disk
To find the unallocated space, choose your disk then run print free
command.
Later we will discuss how to use resizepart command to extend partition when you have more unallocated disk space.
Create new disk partition using parted
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
.
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.
To create a new partition we use mkpart
command with start 0 and end 10000:
To create the second partition, run mkpart command again specifying the start and end size
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 NAME MAJ:MIN RM SIZE RO TYPE MOUNTPOINT 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
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.
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
Set flags on partitions
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
Rescue Linux Disk Partition
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
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 (parted)
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 (parted)
Conclusion
At the time of updating this tutorial, we are using parted 3.1 version, and good to verify the currently supported commands using -h
option. Parted writes directly to the disk, so be cautious when running any commands.
When making any changes, make sure you choose the correct drive otherwise, it may end up in data loss.
If you have any questions or thoughts to share on this topic, please use the below comment section.