1. Home
  2. Partition Management
  3. parted Command Examples

parted Command Examples

The GNU Parted was designed to minimize the chance of data loss. For example, it was designed to avoid data loss during interruptions (like power failure) and performs many safety checks. The parted command examples show you how to build and create partitions in Linux. However, there could be bugs in GNU Parted, so you should back up your important files before running Parted.

NAME: parted – a partition manipulation program

SYNOPSIS

parted [options] [device [command [options…]…]]

DESCRIPTION

parted is a program to manipulate disk partitions. It supports multiple partition table formats, including MS-DOS and GPT. It is useful for creating space for new operating systems, reorganising disk usage, and copying data to new hard disks. This manual page documents parted briefly. Complete documentation is distributed with the package in GNU Info format.

OPTIONS

-h, --help
          displays a help message 
-l, --list
          lists partition layout on all block devices

   -m, --machine
          displays machine parseable output

   -s, --script
          never prompts for user intervention

   -v, --version
          displays the version

   -a alignment-type, --align alignment-type
          Set alignment for newly created partitions, valid alignment types are:

          none  Use the minimum alignment allowed by the disk type.

          cylinder
                 Align partitions to cylinders.

          minimal
                 Use minimum alignment as given by the disk topology information. This and the opt value will use layout  information provided by the disk to align the logical partition table addresses to actual physical blocks on the disks. The min value is the minimum  alignment  needed to align the partition properly to physical blocks, which avoids performance degradation.

          optimal
                 Use optimum  alignment as given by the disk topology information. This aligns to a multiple of the physical block size in a way that guarantees optimal performance.

COMMANDS

[device]
      The block device to be used. When none is given, parted will use the first block device it finds.
[command [options]]
          Specifies the command to be executed. If no command is given, parted will present a command prompt. Possible commands are:

          help [command]
              Print general help, or help on command if specified.

          align-check type partition
               Check if partition satisfies the alignment constraint of type. type must be "minimal" or "optimal".

          mklabel label-type
               Create a new disklabel(partition table)of label-type. label-type should be one of "aix", "amiga", "bsd", "dvh", "gpt", "loop", "mac", "msdos", "pc98", or "sun".

          mkpart [part-type name fs-type] start end
               Create a new partition. part-type may be specified only with msdos and dvh partition tables, it should be one of "primary", "logical", or "extended". name is required for GPT partition tables and fs-type is optional. fs-type can be one of "btrfs", "ext2", "ext3", "ext4", "fat16", "fat32", "hfs", "hfs+", "linux-swap", "ntfs", "reiserfs", or "xfs".

          name partition name
               Set the name of partition to name. This option works only on Mac, PC98, and GPT  disklabels. The name can be placed in double quotes, if necessary. And depending on the shell may need to also be wrapped in single quotes so that the shell doesn't strip off the double quotes.

          print  Display the partition table.

          quit   Exit from parted.

          rescue start end
                 Rescue a lost partition that was located somewhere between start and end.  If a partition is found, parted will ask if you want to create an entry for it in the partition table.

          resizepart partition end
               Change the end position of partition.  Note that this does not modify any filesystem present in the partition.

          rm partition
                 Delete partition.

          select device
                 Choose device as the current device to edit. device should usually be a Linux  hard  disk device,  but it can be a partition, software raid device, or an LVM logical volume if necessary.

          set partition flag state
                 Change the state of the flag on partition to state. Supported flags are: "boot", "root", "swap", "hidden", "raid", "lvm", "lba", "legacy_boot", "irst", "esp" and "palo". state should be either "on" or "off".

          unit unit
                 Set unit as the unit to use when displaying locations and sizes, and for interpreting those given  by  the  user  when not suffixed with an explicit unit. unit can be one of "s" (sectors), "B" (bytes), "kB", "MB", "MiB", "GB", "GiB", "TB", "TiB", "%" (percentage  of  device size), "cyl" (cylinders), "chs"  (cylinders, heads, sectors), or "compact" (megabytes for input, and a human-friendly form for output).

          toggle partition flag
                 Toggle the state of flag on partition.

          version
           Display version information and a copyright message.

SEE ALSO: fdisk(8), mkfs(8), The parted program is fully documented in the info(1) format GNU partitioning software manual.

Parted Command Examples

[root@enlinux ~]# parted
GNU Parted 3.2
Using /dev/sda
Welcome to GNU Parted! Type 'help' to view a list of commands. 

If you type parted then press enter, it will pick the first disk. If you want to partition a specific disk, try to assign the disk label as below example.

[root@enlinux ~]# parted /dev/sdb
GNU Parted 3.2
Using /dev/sdb
Welcome to GNU Parted! Type 'help' to view a list of commands.
(parted)

This is gonna put us into an interactive command prompt. And as you can see, we can type help in order to view a list of commands that we can run.

Create Primary Partitions

The first thing we need to do is create our partition table, and for that, we can use mklabel or mktable.

(parted) mklabel msdos

And then type the partition type that you need. Here in this example, I’m going to use msdos which is the MBR partition table.

The disklabel type must be one of the following: aix, amiga, bsd, dvh, gpt, mac, msdos, pc98, sun, or loop.

Check the updated disk label with help mklabel command.

(parted) help mklabel
mklabel,mktable LABEL-TYPE create a new disklabel (partition table)
LABEL-TYPE is one of: atari, aix, amiga, bsd, dvh, gpt, mac, msdos, pc98, sun, loop

Let’s create a partition with mkpart.

(parted) mkpart primary ext3 1 50000
(parted) mkpart primary ext4 50000 100000

You see, in the above example, I’ve created two primary partitions. The first one is 50G and the second partition space is also 50G.

To check the partitions you created, type print and press enter.

(parted) print
Model: HP LOGICAL VOLUME (scsi)
Disk /dev/sdb: 300GB
Sector size (logical/physical): 512B/512B
Partition Table: msdos
Disk Flags:
Number Start End Size Type File system Flags
1 1049kB 50.0GB 50.0GB primary ext3 lba
2 50.0GB 100GB 50.0GB primary ext4 lba
(parted)
Create Partition with Parted Command in Linux
Create Partition with Parted Command in Linux

The created partitions and all system type is actually not going to display until we actually create the file system on the partition. To create the file system for these partitions, you need to check the mkfs command examples article.

Check the free space on a disk.

(parted) print free

Creating logical partition

Creating logical or extended partition is the same is creating primary partitions. An Extended partition is a special type of partition that contains “Free Space” in which more than the four Primary partitions can be created. Partitions created within the Extended partition are called Logical partitions, and any number of Logical partitions can be created within an Extended partition.

(parted) mkpart extended
Start?
Start? 100000
End? 150000
(parted)

Let’s check the result. Your print out put might be different but you see mine here.

(parted) print
Model: HP LOGICAL VOLUME (scsi)
Disk /dev/sdb: 300GB
Sector size (logical/physical): 512B/512B
Partition Table: msdos
Disk Flags:

Number  Start   End     Size    Type      File system  Flags
 1      1049kB  50.0GB  50.0GB  primary
 2      50.0GB  100GB   50.0GB  primary
 3      100GB   150GB   50.0GB  extended               lba

All done. We’ve completed the process of creating disk partitions in Linux operating systems using partedcommand. Let’s go ahead and remove some partitions.

Remove Partition

The process of deleting a partition is easy a removing a file in Linux. The rm command in parted environment with delete a partition.

Type the rm and follow up with partition number.

(parted) rm 3

Done! Partition 3 has been deleted successfully. Let’s verify the result with the print command.

Delete a Partition In Linux
Delete a Partition In Linux

Finally, type q to quit the parted command-line environment. If you want to find out more information, please see the GNU Parted web page.

Updated on August 30, 2020

Was this article helpful?

Leave a Comment