1

Show disk space with fdisk:

sudo fdisk -l  /dev/sda
Disk /dev/sda: 465.8 GiB, 500107862016 bytes, 976773168 sectors
Disk model: ST500DM002-1BD14
Units: sectors of 1 * 512 = 512 bytes
Sector size (logical/physical): 512 bytes / 4096 bytes
I/O size (minimum/optimal): 4096 bytes / 4096 bytes
Disklabel type: gpt
Disk identifier: 0C7DCAA1-CBD0-4A33-B210-F8D027B84A09

Device         Start       End   Sectors   Size Type
/dev/sda1       2048 390819839 390817792 186.4G Linux filesystem
/dev/sda2  390819840 422070271  31250432  14.9G Linux swap
/dev/sda3  422070272 423120895   1050624   513M EFI System
/dev/sda4  423120896 423153663     32768    16M Microsoft reserved
/dev/sda5  423153664 628613119 205459456    98G Microsoft basic data

The free unused, available space is about:

total space for dev/sda - space for /dev/sda1,/dev/sda2,/dev/sda3,/dev/sda4,/dev/sda5
= 465.8G - 186.4G - 14.9G - 513M - 16M - 98G
= 166G

How can get the number directly with a command?
It is better not to use the method:parse all numbers from fdisk and combine into a calculation expression 465.8 - 186.4 - 14.9 - (513+16/1000) - 98.

2
  • 1
    "It is better not to use the method" Why? In any case, parted /dev/sda print free should help, though it doesn't give you a single number. By the way, I did not know the free option. Thanks to Google (or DuckDuckGo), it took me a minute to learn it. Apr 24, 2021 at 0:26
  • 1
    (976773168 - 628613120) * 512 bytes = 178257944576 bytes free (166 GiB) at the end of the disk
    – Freddy
    Apr 24, 2021 at 0:42

2 Answers 2

2

sfdisk -F /dev/sdX will print both sum of the free space and list of free space areas:

# sfdisk -F /dev/sde
Unpartitioned space /dev/sde: 477.77 MiB, 500973568 bytes, 978464 sectors
Units: sectors of 1 * 512 = 512 bytes
Sector size (logical/physical): 512 bytes / 512 bytes

 Start     End Sectors  Size
 22528   32527   10000  4.9M
 53248   69391   16144  7.9M
 71680  479231  407552  199M
479232 1023999  544768  266M

So if you are interested only in the sum, you can parse it from the first line:

# sfdisk -F /dev/sde | head -1 | cut -d":" -f2 | cut -d"," -f1
 477.77 MiB

Note that calculating free space like this is tricky. Here I have 478 MiB of free space but that doesn't mean I can use it all, I made the partitions in a way that makes the first two free regions unusable (too small to be used) and the space at the end of the disk is not a single continuous free space. This is an exaggerated example, but I've seen worse partitioning than this :-)

If you want to get "biggest free continuous space usable for a partition" you'll need to check starts and ends, partition types etc. and that would be hard to parse from a bash output, you'll probably need to use a library (e.g. libfdisk or libblockdev) to get exact information (which means programming in C or Python).

0

I like the Freddy's way most,all output contains info i'm looking for,it's no nedd to search new tool to get.let me make one more comment here for beginners as me:
Freddy's idea is right,the number is a little wrong,to get the used sector:

List all sectors used:

sudo fdisk -l  /dev/sda |tail -n 5
/dev/sda1       2048 390819839 390817792 186.4G Linux filesystem
/dev/sda2  390819840 422070271  31250432  14.9G Linux swap
/dev/sda3  422070272 423120895   1050624   513M EFI System
/dev/sda4  423120896 423153663     32768    16M Microsoft reserved
/dev/sda5  423153664 628613119 205459456    98G Microsoft basic data

Count all used sectors in the 4th colum:

sudo fdisk -l  /dev/sda |tail -n 5 | awk '{count=count+$4}END{print count}'
628611072

It is 628611072 instead of 628613120.

Free sectors = total sectors - used sectors
            = 976773168 - 628611072
            = 348162096

One sector contains 512 bytes ,free space is :

(976773168 - 628611072)*512/(1024*1024*1024)
=166G

Thank Freddy's idea.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .