Command linux parted – A Complete Guide for Managing Disk Partitions
The parted command in Linux is a powerful tool used for managing disk partitions. Whether you’re setting up a new system, resizing existing partitions, or managing storage space, parted gives you a simple and effective way to interact with your disks. In this article, we’ll dive into what the parted command is, how it works, and provide you with practical examples that will help you make the most out of it. Let’s get started!
What is the parted Command?
In Linux, the parted command is a partition manipulation tool. It allows users to create, delete, resize, and manage disk partitions in a flexible way. Unlike other partitioning tools, parted supports both MBR (Master Boot Record) and GPT (GUID Partition Table) partition schemes, making it versatile for a wide variety of systems. It also supports file systems like ext3, ext4, NTFS, and FAT, among others.
One of the biggest advantages of parted is its ability to handle larger disks and partitions compared to older tools like fdisk, especially with GPT partitions. It offers a simple command-line interface that’s both effective and powerful, making it ideal for advanced Linux users who need to perform disk management tasks efficiently.
Why Use the parted Command?
The parted command is essential for several key disk management tasks. Here are some common reasons you might want to use it:
- Creating new partitions: If you’re setting up a new disk, you’ll use parted to create new partitions based on your requirements.
- Resizing partitions: Over time, you may need to increase or decrease the size of an existing partition. parted allows you to resize partitions without losing data (although, as with any partitioning tool, backing up your data is always recommended).
- Changing file system types: When you need to change the file system on a partition, parted gives you the flexibility to do so.
- Converting partition tables: If you want to convert a disk from MBR to GPT or vice versa, parted can handle the conversion process with ease.
How to Use the parted Command?
The parted command is a bit different from other disk utilities in that it provides a simple interactive mode in addition to its command-line options. Let’s walk through how to use it.
Basic Syntax of parted
The basic syntax of the parted command looks like this:
parted [options]
Here, [options] are any flags or arguments you want to pass to the command, and
sudo parted /dev/sda
Once you’re in parted, you can begin interacting with the disk. If you prefer not to use interactive mode, you can issue commands directly from the terminal with the appropriate options.
Common parted Commands
Here are some commonly used commands in parted that you’ll find helpful:
1. Print the Partition Table
If you want to see the current partition table of a disk, use the print command:
(parted) print
This will display the partitions on the selected disk, along with details such as the partition number, size, type, and file system.
2. Creating a New Partition
To create a new partition, use the mkpart command. Here’s the syntax:
(parted) mkpart
For example, to create a new primary ext4 partition that starts at 0GB and ends at 10GB, you would use:
(parted) mkpart primary ext4 0GB 10GB
The partition-type can be either primary or logical, and file-system-type can be ext4, ntfs, fat32, etc. The start and end are the starting and ending points of the partition, typically expressed in GB, MB, or TB.
3. Deleting a Partition
If you want to delete a partition, you can use the rm command followed by the partition number:
(parted) rm
For example, to delete partition 2, you would use:
(parted) rm 2
4. Resizing a Partition
To resize a partition, use the resizepart command followed by the partition number and the new end point:
(parted) resizepart
For example, if you want to resize partition 1 to end at 20GB, you would use:
(parted) resizepart 1 20GB
It’s important to note that resizing a partition can potentially result in data loss if not done carefully, so always back up important data before performing such operations.
5. Checking the Partition Table
If you want to verify the partition table, use the check command:
(parted) check
This will check the specified partition for errors and attempt to fix any issues.
6. Setting a Partition Label
You can also set labels for partitions using the name command:
(parted) name
For example, to name partition 1 “MyData”, you would use:
(parted) name 1 MyData
Practical Examples of Using parted
Let’s go over a few practical examples of using parted to perform common disk management tasks.
Example 1: Creating a New Partition
Let’s say you have a disk (/dev/sdb) and want to create a new ext4 partition from 0GB to 50GB. Here’s the sequence of commands you would use:
sudo parted /dev/sdb
(parted) mkpart primary ext4 0GB 50GB
(parted) print
This will create a new partition, and you can confirm by printing the partition table to check the new partition's details.
Example 2: Resizing an Existing Partition
Suppose you have a partition (partition 1 on /dev/sda) and want to resize it to 100GB. You would do the following:
sudo parted /dev/sda
(parted) resizepart 1 100GB
(parted) print
This will resize partition 1, and you can verify the change by printing the updated partition table.
Example 3: Deleting a Partition
If you no longer need partition 2 on /dev/sda, you can delete it using:
sudo parted /dev/sda
(parted) rm 2
After deleting, it’s always a good idea to print the partition table to confirm the partition has been removed.
Conclusion
The parted command is a versatile and essential tool for disk partitioning in Linux. Whether you’re creating, deleting, or resizing partitions, parted provides the flexibility and power you need to manage your disks effectively. By understanding its syntax and using it properly, you can make partitioning tasks much easier and more efficient. Remember, disk partitioning is a delicate process, so always back up your data before making any changes. Happy partitioning!

Komentarze (0) - Nikt jeszcze nie komentował - bądź pierwszy!