10+ parted command examples in Linux [Cheat Sheet]

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.

Advertisement

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.

Advertisement
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:

parted command to display the partition table

 

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:

parted command to select different hard disk

 

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:

Advertisement

You need to provide partition type, file system type, start size, and end size as shown below.

parted command to create a primary partition

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:

parted command to create logical partition

 

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:

parted command to resize disk partition

 

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:

parted command to change the flags of partition

 

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'.

parted command to toggle the state of flag on partition

 

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.

parted command to delete a partition

 

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:

parted command to rescue a lost partition

 

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:

parted command to list partition layout on all block devices

 

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:

parted command to create a primary partition

 

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:

parted command to create a logical partition

 

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:

parted command to remove partition

 

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:

parted command to set flags on partition

 

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.

parted command to 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

man page for parted command

 

Didn't find what you were looking for? Perform a quick search across GoLinuxCloud

If my articles on GoLinuxCloud has helped you, kindly consider buying me a coffee as a token of appreciation.

Buy GoLinuxCloud a Coffee

For any other feedbacks or questions you can either use the comments section or contact me form.

Thank You for your support!!

Leave a Comment