How to use the command 'parted' (with examples)

How to use the command 'parted' (with examples)

Parted is a command-line partitioning tool used to manipulate disk partitions on a Linux system. It allows users to create, resize, and remove partitions on block devices. This article will provide examples of different use cases for the ‘parted’ command.

Use case 1: List partitions on all block devices

Code:

sudo parted --list

Motivation:

You may want to list all the partitions on your Linux system to get an overview of the disk layout and the partitions available. This can be useful for troubleshooting or verifying partition configurations.

Explanation:

  • sudo: This command is run with root privileges to have access to all block devices.
  • parted: The command used to launch the partition manipulation program.
  • --list: The option to list all partitions.

Example output:

Model: ATA ST1000DM003-1ER1 (scsi)
Disk /dev/sda: 1000GB
Sector size (logical/physical): 512B/4096B
Partition Table: gpt
Disk Flags:

Number  Start   End     Size    File system     Name  Flags
 1      1049kB  577MB   576MB   fat32                 boot, esp
 2      577MB   7583MB  7007MB  linux-swap(v1)
 3      7583MB  52.9GB  45.3GB  ext4
 4      52.9GB  1000GB  947GB

Use case 2: Start interactive mode with the specified disk selected

Code:

sudo parted /dev/sdX

Motivation:

Starting an interactive session with a specific disk allows direct manipulation of the partitions on that disk. This can be useful when you need to perform multiple operations on a specific disk.

Explanation:

  • sudo: This command is run with root privileges to have access to the specified disk.
  • parted: The command used to launch the partition manipulation program.
  • /dev/sdX: The specific disk (e.g., /dev/sda) you want to select for interactive mode.

Use case 3: Create a new partition table of the specified label-type

Code:

sudo parted --script /dev/sdX mklabel aix|amiga|bsd|dvh|gpt|loop|mac|msdos|pc98|sun

Motivation:

When you want to create a new partition table on a disk, you need to specify the label type for the new table. Different label types offer different functionality and compatibility with different systems.

Explanation:

  • sudo: This command is run with root privileges to have access to the specified disk.
  • parted: The command used to launch the partition manipulation program.
  • --script: The option used to avoid interactive prompts and automatically execute the command.
  • /dev/sdX: The specific disk (e.g., /dev/sda) on which you want to create a new partition table.
  • mklabel: The action to create a new partition table.
  • aix|amiga|bsd|dvh|gpt|loop|mac|msdos|pc98|sun: The label type options you can choose from.

Example output:

Warning: The existing disk label on /dev/sdb will be destroyed and all data on this disk will be lost. Do you want to continue?
Yes/No? Yes

Use case 4: Show partition information in interactive mode

Code:

print

Motivation:

When running ‘parted’ in interactive mode, you may want to check the current partition configuration of the selected disk. This information can help in understanding the layout and characteristics of the partitions.

Explanation:

  • print: The command used to display the partition information of the selected disk.

Example output:

Model: ATA ST1000DM003-1ER1 (scsi)
Disk /dev/sda: 1000GB
Sector size (logical/physical): 512B/4096B
Partition Table: gpt
Disk Flags:

Number  Start   End     Size    File system     Name  Flags
 1      1049kB  577MB   576MB   fat32                 boot, esp
 2      577MB   7583MB  7007MB  linux-swap(v1)
 3      7583MB  52.9GB  45.3GB  ext4
 4      52.9GB  1000GB  947GB

Use case 5: Select a disk in interactive mode

Code:

select /dev/sdX

Motivation:

When running ‘parted’ in interactive mode, you may have multiple disks available and want to choose a specific disk for partition manipulation. This command allows you to select the desired disk.

Explanation:

  • select: The command used to select a specific disk for partition manipulation.
  • /dev/sdX: The specific disk (e.g., /dev/sda) you want to select.

Use case 6: Create a 16 GB partition with the specified filesystem in interactive mode

Code:

mkpart primary|logical|extended btrfs|ext2|ext3|ext4|fat16|fat32|hfs|hfs+|linux-swap|ntfs|reiserfs|udf|xfs 0% 16G

Motivation:

In interactive mode, when you need to create a new partition, you may want to specify its size and file system. This command allows you to create a partition of the desired size and with the specified file system.

Explanation:

  • mkpart: The action to create a new partition.
  • primary|logical|extended: The type of partition you want to create.
  • btrfs|ext2|ext3|ext4|fat16|fat32|hfs|hfs+|linux-swap|ntfs|reiserfs|udf|xfs: The file system to be used for the newly created partition.
  • 0%: The starting position of the partition (e.g., at 0% of the disk).
  • 16G: The size of the partition (e.g., 16 GB).

Use case 7: Resize a partition in interactive mode

Code:

resizepart /dev/sdXN end_position_of_partition

Motivation:

When you need to resize an existing partition in interactive mode, you may want to specify its new size. This command allows you to resize the partition by specifying the end position.

Explanation:

  • resizepart: The action to resize an existing partition.
  • /dev/sdXN: The specific partition (e.g., /dev/sda1) you want to resize.
  • end_position_of_partition: The new end position of the partition (e.g., in sectors or as a percentage).

Use case 8: Remove a partition in interactive mode

Code:

rm /dev/sdXN

Motivation:

When you want to remove a partition in interactive mode, you need to specify the partition you want to delete. This command allows you to remove the specified partition.

Explanation:

  • rm: The action to remove a partition.
  • /dev/sdXN: The specific partition (e.g., /dev/sda1) you want to remove.

Conclusion:

The ‘parted’ command provides a versatile set of tools for managing disk partitions on a Linux system. By following the examples outlined in this article, you can effectively list, create, resize, and remove partitions using the ‘parted’ command.

Related Posts

How to use the command 'nixos-rebuild' (with examples)

How to use the command 'nixos-rebuild' (with examples)

The ’nixos-rebuild’ command is used to reconfigure a NixOS machine. It allows users to build and switch to a new configuration, rollback changes, install updates, and perform other operations related to system configuration.

Read More
How to use the command 'shuf' (with examples)

How to use the command 'shuf' (with examples)

The ‘shuf’ command is used to generate random permutations. It can be used to randomize the order of lines in a file, generate random numbers within a specified range, and more.

Read More
How to use the command numfmt (with examples)

How to use the command numfmt (with examples)

The numfmt command is a tool provided by the Coreutils package that allows you to convert numbers to and from human-readable strings.

Read More