Hardware

Linux commands for hard drive and partition management

Table of contents:

Anonim

Within an organization, one of the responsibilities associated with the IT department is the management and administration of the infrastructure. This activity includes compliance with standards and metrics to ensure proper operation.

The various tasks to be performed include those associated with the performance, status and space of storage devices in operating systems. Regardless of the distro that is being used, we know that it is super important and vital to verify that our disks are not in bad condition, with damaged sectors or without space. All this, to achieve the desired operation.

Taking into account the aforementioned, we will take advantage of this article to take a look at some Linux commands for managing Hard Disk and partitions.

Index of contents

We recommend reading the following two articles:

  • Best SSD of the moment. SSD vs HDD: everything you need to know How to use Cron and Crontab.

Linux Commands for Hard Drive and Partition Management

Fdisk

In case of not having a graphical interface for the management of Hard Disk (something very common in distributions dedicated to servers, since they improve the use of resources), this tool is the ideal one, since it will help us immensely in our tasks.

With this utility we can create, resize, change, delete, move or change partitions using the simple menu it provides. Its only limitation is that it is in 4 maximum primary partitions for each disk, and a number of extended or logical partitions that will change according to the size of the hard disk.

Below we will show some of the most used commands for managing Hard Drive and partitions in the Linux operating system. Of course, we remind you that to make use of these commands, we must do so with a root user or users with similar privileges to avoid inconveniences at the time of executions.

View all partitions

With this we will obtain a list of all the existing partitions in the system. The syntax corresponds to the command along with the argument "-l" and they will be listed in order according to the name of each device.

fdisk –l

View a specific disc

Through this command, we will see all the partitions of a specific disk, we use the previous syntax but adding the name of the device that we want to analyze. For example:

fdisk –l / dev / sdb

See all available commands

fdisk is a fairly comprehensive tool, if you wanted to see all the options available to you, just enter the command followed by the device name to evaluate (a device to work on). Once we enter the menu, press "m" to enter the help section and we will see the list of possibilities that we have to apply on the device.

fdisk / dev / sdb

m

Show entire System partition table

We carry out the same previous process (place the command along with the name of a device), but in this case the option to use is "p" and with this we obtain a complete list of partitions, associated with said device.

fdisk / dev / sdb

p

Delete a partition

If for example, what we want is to delete a specific partition (suppose that / dev / sdb2), the first step is to select the disk where we can locate it. In this case, / dev / sdb.

fdisk / dev / sdb

Once we enter the fdisk menu, press option “d”, corresponding to 'delete ”in the tool. After that, it will request that you enter the number of it has been assigned to the partition that you want to delete.

d

NOTE: Deleting a partition leads to the loss of absolutely all the data that is under its storage, making its recovery almost impossible. Therefore, we must be sure of the list of partitions when performing this action.

Once we are sure of the partition number that we are going to delete, we enter it and the next thing to do is press "w" (for write) to test and confirm the action. The changes will be reflected in the next system startup.

two

w

Create a new partition

We can take advantage of the unused spaces of the hard disk, assigning it to a new partition. The process is very simple. Again, we access the fdisk menu, with the command and the device as parameter.

fdisk / dev / sdb

From the menu, press "n" (new or new) to create the new partition.

n

The next step will be to select the type of partition, that is, whether it will be primary or extended (known as logic). If it is primary, press "p" and for an extended "e".

Next, it will request two data, the first and the last cylinder that will identify the partition. Just put "+ 10000M" in "last cylinder", or which means that + (sum) 10000MB. Like other options, we must write “w” to confirm the changes in the partition table and apply them on a system restart.

Format a partition

We cannot forget that after creating a new partition, we must format it, since it is not an action that is applied by default. For this we introduce the following command:

mkfs.ext4 / dev / sdb5

Where,.ext4 indicates the file format for the partition and sdb5 is a value that changes on each system depending on the partition configuration.

Check the size of a partition

To verify if our new partition is already created and formatted, we can use fdisk with the argument "-s", which will show us the size blocks that said partition occupies. For example:

fdisk -s / dev / sdb5

You may be interested in reading: How is the file system structured in GNU / Linux?

WE RECOMMEND YOU Alternative programs for graphic design in Linux

Correct the partition table

It could be the case that we have deleted intermediate partitions, that is, our disk is partitioned into sdb1, adb2, sdb3, sdb4 and sdb5 and adb3 and sdb4 were deleted. This causes us to receive an error similar to: Partition table entries are not in disk order, indicating that the partitions are not in order by disk and that we have unduly allocated or unallocated space.

The solution is simple, we enter the fdisk menu as I explained in previous sections, with the parameter of the unit to be worked:

fdisk / dev / sdb

Followed by them, press "x" to enter advanced fdisk options. And then press "f" (fix) to tell the tool to repair the partition table. To finish we write "w" and with this, when rebooting, the system will take the changes and our partitions will have been reorganized.

We hope that all this information is useful to you, do not forget to leave us in the comments if it has been useful to you or that other tools you use for managing your hard drive and partitions in your distribution. ?

Hardware

Editor's choice

Back to top button