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.
Interactive Mode
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
Using /dev/sda
Welcome to GNU Parted! Type 'help' to view a list of commands.
(parted)
To exit the parted
shell, you can use the quit
command.
(parted) quit
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.
(parted) print
Sample Output:
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
Sample Output:
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 mklabel
or 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 mkpart
command.
(parted) mkpart
Sample Output:
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 mkpart
command.
(parted) mkpart
Sample Output:
5. Resize disk partition with parted command
The 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
OR
(parted) resizepart partition_number end
Sample Output:
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
OR
(parted) set partition_number flag state
Sample Output:
7. parted command to toggle the state of FLAG on partition
You can toggle the state of the flag on the partition using toggle
command.
(parted) toggle
Sample Output:
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
OR
(parted) rm partition_number
Sample Output:
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
OR
(parted) rescue start end
Sample Output:
10. Set the name of partition with parted command
The name
command can be used to set the name of the partition. This option works only on Mac, PC98, and GPT disklabels.
(parted) name
OR
(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 -l
or --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
OR
$ sudo parted --list
Sample Output:
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
Sample Output:
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
Sample Output:
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
Sample Output:
15. parted command to set the flag on partition
You can change or set the flag on the partition using the set
command.
$ sudo parted /dev/sda set partition_number Flag State
Sample Output:
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%
Sample Output:
It asks you for the closest location parted can manage. If your answer is yes, it will create a partition.
Conclusion
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.
What's Next
10 different methods to check disk space in Linux
Further Reading
Nice BUT
after increasing the size of a used partition (unmounted yet – boot single)
I have no idea how to format the new allocated space without erasing data (which is my home partition with lot of installed apps – and a poor land line).
The resize (of file system) function of parted was removed since v3 (ext).
And I cant use gparted (with verify) on a mounted partition.
So resize without extending formatage is in my case useless …
untill I have a solution.
regards
You can also try this article if it helps https://www.golinuxcloud.com/extend-resize-primary-partition-non-lvm-linux/
solved:
simply after resizing the partition, call resize2fs on existing ext FS to extend it.