partx Command in Linux



The partx command in Linux tells the kernel about the presence and numbering of on-disk partitions. It primarily probes a partition table on a specified device and adds or removes partitions from the partition table.

The partx command is typically used when changes are made to a disk partition layout and the kernel needs to be informed about these changes without rebooting the system.

Table of Contents

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

Syntax of partx Command

The syntax of the Linux partx command is as follows −

partx [options] [device]

The [options] field is used to specify various options to change the command's behavior. The [device] option is used to specify the device name to work on.

partx Command Options

The options of the partx command are listed below −

Flag Option Description
-a --add Add the specified partitions, or read the disk and add all partitions.
-b --bytes Print the sizes in bytes rather than in a human-readable format.
-d --delete Delete the specified partitions or all partitions. It is not an error to remove non-existing partitions.
-g --noheadings Do not print a header line with --show or --raw.
-l --list List the partitions. This output format is DEPRECATED in favor of --show.
-n M:N --nr M:N Specify the range of partitions. Allows negative numbers to specify ranges from the end.
-o list --output list Define the output columns to use for --show, --pairs, and --raw output.
--output-all Output all available columns.
-P --pairs List the partitions using the KEY="value" format.
-r --raw List the partitions using the raw output format.
-s --show List the partitions. Output columns can be selected with --output.
-t type --type type Specify the partition table type.
--list-types List supported partition types and exits.
-u --update Update the specified partitions.
-S size --sector-size size Overwrite default sector size.
-v --verbose Verbose mode.
-h --help Display help text and exit.
-V --version Print version and exit.

Examples of partx Command in Linux

This section describes the usage of the partx command in Linux with examples −

Displaying All Partitions of a Device

To list the partitions of a block device, use the -s or --show option −

sudo partx -s /dev/vda
partx Command in Linux1

Displaying All Partitions in Key-Value Format

To list the partitions of a device in key-value format, use the -P or --pairs option with the partx command −

sudo partx -P /dev/vda
partx Command in Linux2

Displaying Specific Columns

Use the -o or --output option to display specific columns. For example, to show the partition number, starting sector, size, and ending sector, use the command given below −

sudo partx -s -o NR,START,SIZE,END /dev/vda
partx Command in Linux3

The -o option pairs with -s(--show), -P(--pairs), or -r(--raw).

Displaying All Partitions in Bytes

To display the partition in bytes, use the -b or --bytes option −

sudo partx -b /dev/vda
partx Command in Linux4

Listing Supported Partition Types

To list supported partition types, use the --list-types option −

sudo partx --list-types
partx Command in Linux5

Adding All Partitions to Partition Table

To add all the partitions to the kernel's partition table specifically after modifying the partitions using tools like fdisk or parted, use the -a or --add option −

sudo partx -a /dev/vda

After creating a new partition, the kernel may not make a corresponding device file until the partition is added to the kernel's partition table. Using the above command ensures that the new partition is recognized.

Deleting All Partitions from Partition Table

To delete all the partitions to the kernel's partition table, specifically after modifying the partitions using tools like fdisk or parted, use the -d or --delete option −

sudo partx -d /dev/vda

Updating the Kernel Partition Table

To update the kernel partition table with any changes made to the partitions, use the -u or --update option −

sudo partx -u /dev/vda

Getting Verbose Output

To get the verbose output, use the -v or --verbose option −

sudo partx -s -v /dev/vda
partx Command in Linux6

The verbose output gives additional details, for example, in the above output, disk table type, and number of partitions are shown.

Displaying Help

To display help, use the -h or --help option −

partx -h

Conclusion

The partx command in Linux is a handy tool for managing disk partitions by informing the kernel of any changes made to the partition table without requiring a system reboot. It offers various options to add, delete, list, or update partitions and supports features like specifying output formats, listing supported partition types, and operating in verbose mode.

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

Advertisements