Introduction to parted command
parted is a command-line tool in Linux for disk partitioning and partition resizing. You can create, destroy, resize, move and copy ext2, linux-swap, FAT, FAT32, and reiserfs partitions. It helps create space for new operating systems, reorganizing disk usage and copying data to new hard disks. You need to be careful when managing the disk partitions because the data can be overwritten or lost.
How to install parted in Linux
In most Linux distributions, parted is pre-installed in the system. However, if it is not present in the system, you can use the following command to install parted in the Linux system.
To install parted on CentOS, Fedora, and RHEL
$ sudo yum install parted
To install parted on Ubuntu and Debian
$ sudo apt-get install parted
Syntax for parted command
The syntax for the
parted command is:
$ parted [options] [device [command [options...]...]]
Some options available in
parted command are as follows.
- -l: to list partition layout on all block devices
- -m: to display machine parseable output
- -v: to display the version of the program
- -h: to display a help message
Different examples to use parted command
parted command can be used in two modes: interactive mode and command line mode. This tutorial will introduce both methods to use parted command.
When parted is used without any options, it displays the version of the parted program and opens the parted in interactive mode. You can use different parted commands in its interactive mode.
ubuntu@golinux:~$ sudo parted
GNU Parted 3.3
Welcome to GNU Parted! Type 'help' to view a list of commands.
To exit the
parted shell, you can use the
1. parted command to display the partition table
The print command is used to print the partition table. It also displays the hard disk model and sizes.
2. Select different hard disk with parted command
If you have multiple hard disks in the system, you can easily switch between them using the
select command. The selected disk will be used for partition and other purposes.
(parted) select /disk_name
3. parted command to create a primary partition
In interactive mode, the command
mkpart is used to create a new partition. Before creating a new partition, you need to create a partition table. To create a partition table, you can use
mktable command followed by partition type. Here, we are creating an MBR partition table, msdos.
(parted) mklabel msdos
After that, you can create a new partition with the
You need to provide partition type, file system type, start size, and end size as shown below.
As you can see above, the primary partition is created starting at 1000kB and ending at 20GB.
You can also provide all details in one command as shown below a primary partition from the command line.
(parted) mkpart primary ext2 1 20000
4. parted command to create a logical partition
Similarly, you can create a logical partition with the help of the
5. Resize disk partition with parted command
resizepart command helps to resize disk partition in the Linux system. You need to provide the partition number and end size to resize the particular partition.
(parted) resizepart partition_number end
6. Change the FLAG on partition with parted command
You can change the flag on the partition using the
set command. The supported flags are: "boot", "root", "swap", "hidden", "raid", "lvm", "lba", "legacy_boot" and "palo". The state should be either 'on' or 'off'.
(parted) set partition_number flag state
7. parted command to toggle the state of FLAG on partition
You can toggle the state of the flag on the partition using
The state 'on' is changed to 'off'.
8. parted command to delete the partition
Sometimes, you may need to remove the partition. You can use
rm command to delete the disk partition in the Linux system.
(parted) rm partition_number
As you can see, partition number 2 is removed.
9. Rescue a lost partition with parted command
You can use the
rescue command to recover a lost partition near start and end. If the partition is found between that range, parted will try to recover it.
(parted) rescue start end
10. Set the name of partition with parted command
name command can be used to set the name of the partition. This option works only on Mac, PC98, and GPT disklabels.
(parted) partition_number name
Non-Interactive Mode (One Liner parted commands)
In the command line, you can use parted commands directly without entering the interactive shell.
11. parted command to list the partition layout on all block devices
You can use
--list option to view or list the partition layout on all block devices. It displays the model of the hard disk, disk size, disk flags, and partition table.
$ sudo parted -l
$ sudo parted --list
12. parted command to create a primary partition
You can use the following command to create a primary partition in the disk.
$ sudo parted /dev/sda mkpart primary ext2 start end
13. parted command to create a logical partition
Similarly, you can create a logical partition in the disk by running the command below.
$ sudo parted /dev/sda mkpart extended start end
14. parted command to remove a partition
You can run the following command as shown below to remove a partition.
$ sudo parted /disk/name rm partition_number
15. parted command to set the flag on partition
You can change or set the flag on the partition using the
$ sudo parted /dev/sda set partition_number Flag State
16. parted command to create a partition without knowing disk size
If you don't know the start size while creating a partition, you can fill the disk to the end using 100%.
For example, to create a primary partition of 5GB, you can use the command below.
$ sudo parted /dev/sda mkpart primary 5000 100%
It asks you for the closest location parted can manage. If your answer is yes, it will create a partition.
This leads to the end of our tutorial on parted command in Linux. parted is a helpful and powerful tool for managing disk partitions in the Linux system. If you still have any confusion, please feel free to ask us in the comment section.