parted Command in Linux



The parted command in Linux is a disk partitioning tool used to create, delete, resize, and manage disk partitions and file systems. It is particularly useful for managing large storage devices and supports both MS-DOS (MBR) and GPT partition tables. It is a handy tool for allocating space for new operating systems, rearranging disk usage, and transferring data to new hard drives.

Table of Contents

Here is a comprehensive guide to the options available with the parted command −

Syntax of parted Command

The syntax of the parted command in Linux is as follows −

parted [options] [device] [command [arguments]]

In the above syntax −

  • [options] − Optional flags to modify the behavior of the parted command.
  • [device] − Used to specify the storage device to manage such as /dev/sda or /dev/vda.
  • [command] − Used to specify the actions to perform on the device, such as creating partitions, resizing them, or printing partition details.
  • [arguments] − Additional parameters required by the command.

parted Command Options

The options for the parted command are listed below −

Flag Option Description
-l --list Lists partition layouts on all block devices.
-m --machine Shows machine-parsable output.
-j --json Outputs data in JSON format.
-s --script Prevents interactive prompts.
-f --fix Automatically fixes exceptions in script mode.
-a --align Sets alignment for new partitions (none, cylinder, minimal, optimal).
-h --help Displays a help message.
-v --version Displays the version of the tool.

Examples of parted Command in Linux

In this section, the usage of the parted command in Linux will be discussed with examples −

  • Creating a Partition using parted Command
  • Deleting a Partition
  • Resizing a Partition
  • Creating a New Partition Label
  • Exiting the parted Interactive Interface
  • Displaying the Partition Information in JSON Format
  • Displaying the Partition Layouts
  • Displaying the Partition in Machine Readable Format
  • Displaying Usage Help

Creating a Partition using parted Command

To create a new partition using the parted tool on Linux follow the steps mentioned below −

Run the parted command with the device name that needs to be parted −

sudo parted /dev/vda3

Note − The parted command requires sudo privileges.

The above command opens an interactive interface as shown in the image below −

Parted Command in Linux1

Type help and then press the Enter key to display all the commands that can be used for disk management. The list of commands with descriptions is given below −

Command Description
align-check TYPE N Check partition N for alignment (min|opt).
help [COMMAND] Display general help or help for a specific command.
mklabel,mktable LABEL-TYPE Create a new disk label (partition table).
mkpart PART-TYPE [FS-TYPE] START END Create a new partition with specified type and file system.
name NUMBER NAME Assign a name to partition NUMBER.
print [devices|free|list, all] Show the partition table or information about available devices or free space.
quit Exit the program.
rescue START END Recover a lost partition near the specified start and end points.
resizepart NUMBER END Resize partition NUMBER to the specified end point.
rm NUMBER Delete partition NUMBER.
select DEVICE Choose the device to edit.
disk_set FLAG STATE Change the FLAG on the selected device.
disk_toggle [FLAG] Toggle the state of FLAG on the selected device.
set NUMBER FLAG STATE Change the FLAG on partition NUMBER.
toggle [NUMBER [FLAG]] Toggle the state of FLAG on partition NUMBER.
type NUMBER TYPE-ID or TYPE-UUID Set the TYPE-ID or TYPE-UUID for partition NUMBER.
unit UNIT Set the default unit to UNIT.
version Display version number and copyright information for GNU Parted.

To create a new partition, use the mkpart command −

mkpart primary ext4 1GB 3GB

The above command creates a primary partition type ext4 filesystem type. The partition will begin at 1GB from the start of the disk and end at 3GB on the disk, meaning the partition size will be 2GB.

Deleting a Partition

To delete a partition, use the rm command with the partition number in the parted interactive interface. To list the partitions, use the print command with the list argument −

print list
Parted Command in Linux2

For example, to delete the second partition, use the following command −

rm 2

The 2 represents the partition number.

Resizing a Partition

To resize the partition, use the resizepart command. For example, to resize the partition 3 to 2GB, use the following command in the parted command interface −

resizepart 3 2GB

Creating a New Partition Label

Creating a new partition table removes all existing partitions and data from the disk, so it should be used with caution. To create a label in the parted command interface, use the following command −

parted mklabel gpt

Another option for the label is msdos which is older.

Exiting the parted Interactive Interface

To quit the parted command interactive interface, use the quit command −

quit
Parted Command in Linux3

In the following examples, the parted command is being used in a non-interactive way.

Displaying the Partition Information in JSON Format

To display the partition information in JSON format, use the -j or --json option −

sudo parted -j /dev/vda1 print
Parted Command in Linux4

The above command outputs partition information in JSON format, which is useful for automated processing and scripting.

Displaying the Partition Layouts

To display the partition layouts on all block devices, use the -l or --list option with the parted command −

sudo parted -l
Parted Command in Linux5

Displaying the Partition in Machine Readable Format

To display the partition in machine-readable format, use the -m or --machine option −

sudo parted -m /dev/vda2 print
Parted Command in Linux6

This is important because it provides output that can be easily parsed by scripts or other automation tools, making it ideal for processing data without human intervention.

Displaying Usage Help

To display the usage help of the parted command, use the -h or --help option −

parted -h

Conclusion

The parted command in Linux is a handy tool for managing disk partitions. It allows users to create, delete, resize, and organize partitions on storage devices, offering support for both MS-DOS and GPT partition tables.

By using commands like mkpart, rm, and resizepart, managing storage becomes efficient, especially for handling large devices. The tool also provides options to display partition information in different formats, automate tasks, and manage device layouts

In this tutorial, we covered the parted command, its syntax, options, and usage in Linux with examples.

Advertisements