Check disk partition in linux

10 fdisk Commands to Manage Linux Disk Partitions

fdisk stands (for “fixed disk or format disk“) is an most commonly used command-line based disk manipulation utility for a Linux/Unix systems. With the help of fdisk command you can view, create, resize, delete, change, copy and move partitions on a hard drive using its own user friendly text based menu driven interface.

This tool is very useful in terms of creating space for new partitions, organising space for new drives, re-organising an old drives and copying or moving data to new disks. It allows you to create a maximum of four new primary partition and number of logical (extended) partitions, based on size of the hard disk you have in your system.

fdisk command to manage disk partition

This article explains 10 basic fdisk commands to manage a partition table in Linux based systems. You must be root user to run fdisk command, otherwise you will get a “command not found” error.

1. View all Disk Partitions in Linux

The following basic command list all existing disk partition on your system. The ‘-l‘ argument stand for (listing all partitions) is used with fdisk command to view all available partitions on Linux. The partitions are displayed by their device’s names. For example: /dev/sda, /dev/sdb or /dev/sdc.

2. View Specific Disk Partition in Linux

To view all partitions of specific hard disk use the option ‘-l‘ with device name. For example, the following command will display all disk partitions of device /dev/sda. If you’ve different device names, simple write device name as /dev/sdb or /dev/sdc.

3. Check all Available fdisk Commands

If you would like to view all commands which are available for fdisk. Simply use the following command by mentioning the hard disk name such as /dev/sda as shown below. The following command will give you output similar to below.

Type ‘m‘ to see the list of all available commands of fdisk which can be operated on /dev/sda hard disk. After, I enter ‘m‘ on the screen, you will see the all available options for fdisk that you can be used on the /dev/sda device.

4. Print all Partition Table in Linux

To print all partition table of hard disk, you must be on command mode of specific hard disk say /dev/sda.

From the command mode, enter ‘p‘ instead of ‘m‘ as we did earlier. As I enter ‘p‘, it will print the specific /dev/sda partition table.

5. How to Delete a Partition in Linux

If you would like to delete a specific partition (i.e /dev/sda9) from the specific hard disk such as /dev/sda. You must be in fdisk command mode to do this.

Next, enter ‘d‘ to delete any given partition name from the system. As I enter ‘d‘, it will prompt me to enter partition number that I want to delete from /dev/sda hard disk. Suppose I enter number ‘4‘ here, then it will delete partition number ‘4‘ (i.e. /dev/sda4) disk and shows free space in partition table. Enter ‘w‘ to write table to disk and exit after making new alterations to partition table. The new changes would only take place after next reboot of system. This can be easily understood from the below output.

Читайте также:  Темная тема для winrar windows 10

Warning : Be careful, while performing this step, because using option ‘d‘ will completely delete partition from system and may lost all data in partition.

6. How to Create a New Partition in Linux

If you’ve free space left on one of your device say /dev/sda and would like to create a new partition under it. Then you must be in fdisk command mode of /dev/sda. Type the following command to enter into command mode of specific hard disk.

After entering in command mode, now press “n” command to create a new partition under /dev/sda with specific size. This can be demonstrated with the help of following given output.

While creating a new partition, it will ask you two options ‘extended‘ or ‘primary‘ partition creation. Press ‘e‘ for extended partition and ‘p‘ for primary partition. Then it will ask you to enter following two inputs.

  1. First cylinder number of the partition to be create.
  2. Last cylinder number of the partition to be created (Last cylinder, +cylinders or +size).

You can enter the size of cylinder by adding “+5000M” in last cylinder. Here, ‘+‘ means addition and 5000M means size of new partition (i.e 5000MB). Please keep in mind that after creating a new partition, you should run ‘w‘ command to alter and save new changes to partition table and finally reboot your system to verify newly created partition.

7. How to Format a Partition in Linux

After the new partition is created, don’t skip to format the newly created partition using ‘mkfs‘ command. Type the following command in the terminal to format a partition. Here /dev/sda4 is my newly created partition.

8. How to Check Size of a Partition in Linux

After formatting new partition, check the size of that partition using flag ‘s‘ (displays size in blocks) with fdisk command. This way you can check size of any specific device.

9. How to Fix Partition Table Order

If you’ve deleted a logical partition and again recreated it, you might notice ‘partition out of order‘ problem or error message like ‘Partition table entries are not in disk order‘.

For example, when three logical partitions such as (sda4, sda5 and sda6) are deleted, and new partition created, you might expect the new partition name would be sda4. But, the system would create it as sda5. This happens because of, after the partition are deleted, sda7 partition had been moved as sda4 and free space shift to the end.

To fix such partition order problems, and assign sda4 to the newly created partition, issue the ‘x‘ to enter an extra functionality section and then enter ‘f‘ expert command to fix the order of partition table as shown below.

After, running ‘f‘ command, don’t forget to run ‘w‘ command to save and exit from fdisk command mode. Once it fixed partition table order, you will no longer get error messages.

10. How to Disable Boot Flag (*) of a Partition

By default, fdisk command shows the boot flag (i.e. ‘*‘) symbol on each partition. If you want to enable or disable boot flag on a specific partition, do the following steps.

Press ‘p‘ command to view the current partition table, you see there is a boot flag (asterisk (*) symbol in orange color) on /dev/sda1 disk as shown below.

Next enter command ‘a‘ to disable boot flag, then enter partition number ‘1‘ as (i.e. /dev/sda1) in my case. This will disable boot flag on the partition /dev/sda1. This will remove the asterisk (*) flag.

I’ve tried my best to include almost all basic commands of fdisk commands, but still fdisk contains a variety of other expert commands you can use them by entering ‘x‘. For more detailed information, check out ‘man fdisk‘ command from the terminal. If I’ve missed any important command, please do share with me via comment section.

Read Also :

If You Appreciate What We Do Here On TecMint, You Should Consider:

TecMint is the fastest growing and most trusted community site for any kind of Linux Articles, Guides and Books on the web. Millions of people visit TecMint! to search or browse the thousands of published articles available FREELY to all.

If you like what you are reading, please consider buying us a coffee ( or 2 ) as a token of appreciation.

Читайте также:  Моргает рабочий стол windows 10 решаем за минуту

We are thankful for your never ending support.

Источник

10 different methods to check disk space in Linux

Table of Contents

In this article I will share different commands and methods to check disk space in Linux. Most of these commands and methods should be available on most Linux distros. You can also utilise these commands to create a script to monitor disk size usage and create alarms or send mails (this would need a mail server).

1. Check partition size using df command

df is one of the most used command to check space of available partitions on the Linux setup. You can use df wih -Th to print the partition type and the partition size in human readable format. This command will show you the total available, used and free space per partition.

You can also use other arguments such as -i to print the available, used and free inode count per partition:

You can check the man page of df command for other supported arguments to check disk space in different formats.

2. Check disk space using fdisk utility

fdisk is another very handy utility for system administrators. fdisk is a user interactive program mostly used for creation and manipulation of partition tables such as creating, deleting, modifying partitions. But we can also use this utility to list the available disks connected to the Linux system along with the respective disk size.

Here is a snippet of output from command » fdisk -l »

As you can see from the output, I have a disk /dev/sda with a size of 15GB. The output also shows more details about this disks wherein it contains two partitions /dev/sda1 and /dev/sda2 .

The output of fdisk -l can be quiet long so if you just wish to check the disk space then you can use this command:

Here I have combined fdisk with awk to only print required sections. So I have two disks with 15GB and 8GB each, while I have two LVM partitions which are most likely part of these disks with a size of 14GB and 512MB.

3. Check disk space using parted utility

parted is another alternative to fdisk and is also used for manipulating disk partitions. It is useful for creating space for new operating systems, reorganising disk usage, and copying data to new hard disks.

Similar to fdisk , we can use parted -l to list all the available disks along with their respective size:

If you compare the output of parted with fdisk , here we only get the available disk and their size and the LVM details are missing which is a good thing as we only wanted to check the disk space.

We can also use awk with parted to further only print required output:

4. Check file size using du command

du is another wonderful utility to check the size of files recursively in Linux. It can also summarize disk usage of the set of files, recursively for directories. du is a very versatile tool and supports multiple arguments which you can use based on your requirement. Some of the most used arguments are:

Here are some example demonstrating different scenarios to check file system size using du command:

EG-1: Check size of all the files under a partition recursively

I have a separate partition for /boot :

So let’s check the size of all the files under /boot using du -h /boot/*

EG-2: Print total summary of size of files in a partition or directory

We can use du -c to print a total or summary of all the file size under provided directory or partition.

So the total file size under /etc/iscsi directory is 20K . You can also use this command under root i.e. du -sch /* to get a summary and total size of all the files under /

EG-3: Sort the output based on file size

By default the du command will print the size of the file based on the first available directory or sub-directory. So the output is not sorted and it can be a tedious task to go through the long list. We can combine du with sort command do sort the output based on the file size.

Here we have used du with sort -hr where -h is used to compare human readable numbers such as 2K, 1G, 4M etc while -r is used to reverse the order of search.

EG-4: Print file size larger than specified size

By default du will print the size of every file found under a partition or directory. We can add a threshold to print files higher than a certain size.

Читайте также:  Скорость загрузки linux без systemd

For example here we are printing the files with size higher than 1MB under /var/log

For more list of supported options check the man page of du command.

5. Check disk size using lsblk command

lsblk lists information about all available or the specified block devices. The lsblk command reads the sysfs filesystem and udev db to gather information. If the udev db is not available or lsblk is compiled without udev support than it tries to read LABELs, UUIDs and filesystem types from the block device.

We can use lsblk with -o LIST or —output LIST to get desired columns based on the value of LIST. Use lsblk —help to get a list of all supported columns.

We will use name, fstype, size, mountpoint LIST for our example. Although using only size and name was enough but to give more detailed output I am using additional list options.

6. Print disk size using blockdev

We can use blockdev command to print a report for the specified device. It is possible to give multiple devices. If none is given, all devices which appear in /proc/partitions are shown. Note that the partition StartSec is in 512-byte sectors.

You can also use following commands:

7. Check disk size and details using lshw command

lshw is a small tool to extract detailed information on the hardware configuration of the machine. It can report exact memory configuration, firmware version, mainboard configuration, CPU version and speed, cache configuration, bus speed, etc. on DMI-capable x86 or IA-64 systems and on some PowerPC machines.

By default lshw will give you a huge output, we can limit that by only printing the information for a specified class such as » disk «. To get the list of attached disks and their details such as size we will use lshw -c disk .

Here is an output snippet from my Linux node:

We can further improve the output using lshw with grep :

8. Checking disk size from the system logs

We can also use system logs such as boot logs using dmesg or journalctl -b and try to search for respective disk to get more information. The catch is that you should know the disk name unlike other methods which we discussed where we were able to check size of all the available disks without prior knowledge of disk name.

For example, here I am searching for all instance of sda disk in boot logs using dmesg :

Similarly we can search for any other disk, such as sdb :

You may also try grepping blocks in dmesg output which can list the available disk with their size:

9. Check disk size using lsscsi command

lsscsi uses information in sysfs (Linux kernel series 2.6 and later) to list SCSI devices (or hosts) currently attached to the system. Many non-SCSI storage devices (but not all) used the SCSI subsystem in Linux. In lsscsi version 0.30 support was added to list NVMe devices.

We can use lsscsi —size to list all the connected storage devices along with their size as shown below:

10. Print disk size using sfdisk

sfdisk is again an alternative to fdisk and parted utility. One of the major difference between fdisk and sfdisk is that sfdisk reads and writes partition tables, but is not interactive like fdisk or cfdisk (it reads input from a file or stdin). It’s generally used for partitioning drives from scripts or for partition table backup and recovery.

But it can also be used to check the disk size using sfdisk -l , sample output:

Summary

In this article we explored different commands and methods which can be used to check disk size (used/available/total) in Linux. You can easily utilize most of these commands and methods into a script to regularly monitor the available disk space and raise alarm when threshold is reached. There are some more commands such as udisks, hwinfo which can be used to collect similar information but they are distro dependent and may not be available with all the Linux distributions hence I have skipped them.

Didn’t find what you were looking for? Perform a quick search across GoLinuxCloud

If my articles on GoLinuxCloud has helped you, kindly consider buying me a coffee as a token of appreciation.

For any other feedbacks or questions you can either use the comments section or contact me form.

Thank You for your support!!

Источник

Оцените статью