How to Check Unallocated Space on Linux

Written by: Bobbin Zachariah   |   Last updated: June 27, 2022

Unallocated space is the unused space on the hard disk which has not been partitioned into a volume or drive. It is not a partition, just dead space. It can be made into a partition and formatted or it can be used to extend a current partition next to it.

In this guide, I will show you commands which can help to check unallocated disk space on your Linux system in order to extend the existing partition or create a new disk.

What is unallocated space?

Don't confuse free space and unallocated space. Unallocated space means that the operating system knows that there's physical space on the hard drive, but it hasn't been assigned (allocated) to a particular partition whereas free space means that the operating system knows you haven't used up all of the allocated space on a particular drive or partition.

1. Display disk cylinders

With fdisk command, the start and end columns in your fdisk -l output are the start and end cylinders. From the header of your fdisk -l output, you can also see how many cylinders the disk has and how many bytes a cylinder represents. Normally, the cylinder's values follow each other when all disk space is allocated. If there are some values that are missing, it means that we have unallocated disk space.

Consider the example below

# fdisk -l /dev/sdf

Disk /dev/sdf: 12.9 GB, 12884901888 bytes, 25165824 sectors
Units = sectors of 1 * 512 = 512 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disk label type: dos
Disk identifier: 0x7e5db80f

   Device Boot      Start         End      Blocks   Id  System
/dev/sdf1            2048     9766911     4882432   83  Linux
/dev/sdf2        18032550    25165823     3566637   83  Linux

You can see that /dev/sdf1 ends on sector '9766911' but /dev/sdf2 doesn't start directly on the next value but on sector '18032550'It means that the range '9766912-18032549' is missing so there is unallocated space on the disk. To calculate the unallocated space, you can do as below:

  • missing sectors: 18032549 - 9766912 = 82656378265637
  • calculate the size in bytes: 82656378265637 * 512 = 4232006144 bytes ~ 4.2GB

Notice that /dev/sdf1 starts on sector '2048' which is normal on some cases for the first default partition. It also means that before, there are few unallocated space.

2. Show numbering of on-disk partitions

It is possible to use partx command which asks the kernel to probe a given device and re-read the partition table. Given a device or disk-image, partx tries to parse the partition table and list its contents. You first need to take disk size information with fdisk -l command

# fdisk -l /dev/sdf

Disk /dev/sdf: 12.9 GB, 12884901888 bytes, 25165824 sectors
Units = sectors of 1 * 512 = 512 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disk label type: dos
Disk identifier: 0x7e5db80f

   Device Boot      Start         End      Blocks   Id  System
/dev/sdf1            2048     9766911     4882432   83  Linux
/dev/sdf2        18032550    25165823     3566637   83  Linux

Total space 12.9 GB . You can use partx -l command to list the partitions in order to find the used space and can subtract from total space. You shall notice that but all numbers are in 512-byte sectors.

# partx -l /dev/sdf
# 1: 2048-      9766911 ( 9764864 sectors, 4999 MB)
# 2: 18032550- 25165823 ( 7133274 sectors, 3652 MB)

#1 and #2 are the number of partitions. Remember that in Linux, when a disk space is ready to be initialized by the system, it is numbered as you can see on fdisk -l output. So my server has around 4.2 GB unallocated space.

Instead of -l option, you can use -s option to have more information detailed and size readable by human.

# partx -s /dev/sdf
NR START         END SECTORS SIZE NAME UUID
 1 2048      9766911 9764864 4.7G 
 2 18032550 25165823 7133274 3.4G

3. Use the partition manipulation program

Another option we have is to use parted command. You can use 'free' parameter to get unallocated space and also disk partition.

# parted /dev/sdf print free
Model: VMware, VMware Virtual S (scsi)
Disk /dev/sdf: 12.9GB
Sector size (logical/physical): 512B/512B
Partition Table: msdos
Disk Flags: 

Number  Start   End     Size    Type     File system  Flags
        32.3kB  1049kB  1016kB           Free Space
 1      1049kB  5001MB  5000MB  primary  ext2
        5001MB  9233MB  4232MB           Free Space
 2      9233MB  12.9GB  3652MB  primary

The keyword Free Space there doesn't refer to the free space explained on the introduction but exactly to unallocated space which is the subject of this topic. You can see the first Free Space which refers to the unallocated space that I explained before for the first default partition. Also notice that unallocated space has no numbering.

In the following example, there is no partition on the hard disk and so it displays only unallocated space (free space). See that there is no numbering

# parted /dev/sdg print free
Model: VMware, VMware Virtual S (scsi)
Disk /dev/sdg: 10.7GB
Sector size (logical/physical): 512B/512B
Partition Table: msdos
Disk Flags: 

Number Start   End     Size   Type  File system   Flags
       32.3kB  10.7GB  10.7GB       Free Space

When you execute parted command without any argument, by default it selects the first hard disk drive that is available on your system.

To print free space in specific units, for example to print in GB:

# parted /dev/sda unit GB print free

4. Display disk partition table

Another command we can use is cfdisk.  It is a curses-based program for partitioning any hard disk drive. The partitions section always displays the current partition table. The command line is the place where commands and text are entered. The format of the partition table in the partitions section is, from left to right: Name, Flags, Partition Type, Filesystem Type, and Size.

# cfdisk /dev/sdf

You can see the first row shows Free Space. For more detail, you can choose 'Print', then 'Sectors' and validate to have the result below

Conclusion

In this guide, we have learned how to find unallocated disk space using Linux terminal commands. Among the command, I feel parted command is the simplest solution to find unallocated space.

Thanks for reading, please provide your suggestion in the below comment section.

About The Author

Bobbin Zachariah

Bobbin Zachariah

Bobbin Zachariah is an experienced Linux engineer who has been supporting infrastructure for many companies. He specializes in Shell scripting, AWS Cloud, JavaScript, and Nodejs. He has qualified Master’s degree in computer science. He holds Red Hat Certified Engineer (RHCE) certification and RedHat Enable Sysadmin.

SHARE

Comments

Please add comments below to provide the author your ideas, appreciation and feedback.

Leave a Reply

Leave a Comment