69

I want to view the unallocated free space on my hard disk through terminal. I've burned my brains searching the internet for a possible solution, but all in vain.

I used all sorts of commands like df, du, fdisk, parted, etc. It tells me about the disks that are mounted and unmounted, but what about the unallocated space that I've left free?

Of course I can view it using the 'Disk Utility' app provided by Fedora, but since I LOVE being in the terminal I'd like to view in it.

Can anyone please help me with a solution?

3
  • You might have a misunderstanding about free space. Don't forget overhead, and do remember that open(2)-ed but unlink(2)-ed files still use disk space. Sep 8, 2012 at 9:45
  • This may be considered off-topic for Stack Overflow. You might want to ask about this on Unix.SE or Super User (but search for it there first!). Jan 9, 2013 at 5:01
  • I think also server fault (serverfault.com) would be more appropriate
    – a1an
    Jul 25, 2013 at 15:02

13 Answers 13

93

Use GNU parted and print free command:

root@sandbox:~# parted
GNU Parted 2.3
Using /dev/sda
Welcome to GNU Parted! Type 'help' to view a list of commands.
(parted) print free
Model: VMware Virtual disk (scsi)
Disk /dev/sda: 64.4GB
Sector size (logical/physical): 512B/512B
Partition Table: msdos

Number  Start   End     Size    Type      File system  Flags
        32.3kB  1049kB  1016kB            Free Space
 1      1049kB  256MB   255MB   primary   ext2         boot
        256MB   257MB   1048kB            Free Space
 2      257MB   64.4GB  64.2GB  extended
 5      257MB   64.4GB  64.2GB  logical                lvm
        64.4GB  64.4GB  1049kB            Free Space
3
  • 23
    what command should i do to use the free space ? Nov 14, 2014 at 6:21
  • It's the bit after (parted), this is your prompt, so the following command: print free
    – Tisch
    Jul 11, 2017 at 20:24
  • The command to use/reclaim that free space depends on where that free space is located, between existing partitions, and what kind of partitions you have (primary, extended) and also if you use LVM or not. It's not trivial to just run a simple command that will read your mind and know what you want to actually do with that free space :)
    – Mladen B.
    Mar 4, 2021 at 9:44
34

To see in TB:

# parted /dev/sda unit TB print free | grep 'Free Space' | tail -n1 | awk '{print $3}'

To see in GB:

# parted /dev/sda unit GB print free | grep 'Free Space' | tail -n1 | awk '{print $3}'

To see in MB:

# parted /dev/sda unit MB print free | grep 'Free Space' | tail -n1 | awk '{print $3}'

To see in bytes:

# parted /dev/sda unit B print free | grep 'Free Space' | tail -n1 | awk '{print $3}'

To see in %:

# parted /dev/sda unit '%' print free | grep 'Free Space' | tail -n1 | awk '{print $3}'

To see in sectors:

# parted /dev/sda unit s print free | grep 'Free Space' | tail -n1 | awk '{print $3}'

Change /dev/sda to whatever device you are trying to find the information about. If you are using the result in any calculations, make sure to trim the trailing characters.

2
  • By adding the --machine argument to the parted invocation, you get colon separated output which will be much more reliable to parse than the default human readable output.
    – josch
    Mar 17, 2023 at 12:19
  • With the parted commands like this, parted will run in interactive mode. If this isn't intended, the "unit TB" and "print free" commands need to swap places. This is also how the --help option shows it: first the command, then command args. The full command would then be: parted /dev/sda print free unit TB
    – jmk
    Jun 21, 2023 at 9:20
12

The simplest way to show unallocated free space in a single command:

$ sudo sfdisk --list-free /dev/sdX

(Add the --quiet option if you don't need the extra info about sector size, etc.)

2
  • 1
    This is great, thanks. If you don't specify a disk, it will list all of them!
    – grilix
    May 23, 2021 at 0:31
  • By adding the --json option to sfdisk one can get output that is actually machine redable.
    – josch
    Mar 17, 2023 at 12:22
6

In addition to all the answers about how to find unpartitioned space, you may also have space allocated to an LVM volume but not actually in use. You can list physical volumes with the pvdisplay and see which volume groups each physical volume is associated with. If a physical volume isn't associated with any volume group, it's safe to reallocate or destroy. Assuming that it it is associated with a volume group, the next step is to use vgdisplay to show your those. Among other things, this will show if you have any free "physical extents" — blocks of storage you can assign to a logical volume. You can get this in a concise form with vgs:

$ sudo vgs
  VG     #PV #LV #SN Attr   VSize   VFree
  fedora   1   3   0 wz--n- 237.46g    0 

... and here you can see I have nothing free. If I did, that last number would be bigger than zero.

This is important, because that free space is invisible to du, df, and the like, and also will show up as an allocated partition if you are using fdisk or another partitioning tool.

1
  • vgdisplay really was the answer for me, as with most installers today a great option is to go with encrypted LVM which creates volume groups with logical volumes within. Then when moving space around the unallocated space is in the volume group, not the physical volume partitions.
    – 13013SwagR
    Apr 1, 2021 at 14:50
6

I had just the same trouble with fedora 26 and LVM partitions, it seems I forgot check something during the installation, So, my 15G root directory has been increased to 227G like I needed.

I posted the steps I followed here:

resize2fs: Bad magic number in super-block while trying to open

0) #df -h

Filesystem               Size  Used Avail Use% Mounted on
devtmpfs                 1.9G     0  1.9G   0% /dev
tmpfs                    1.9G     0  1.9G   0% /dev/shm
tmpfs                    1.9G  824K  1.9G   1% /run
tmpfs                    1.9G     0  1.9G   0% /sys/fs/cgroup
/dev/mapper/fedora-root   15G  2.1G   13G  14% /
tmpfs                    1.9G     0  1.9G   0% /tmp
/dev/md126p1             976M  119M  790M  14% /boot
tmpfs                    388M     0  388M   0% /run/user/0

1) # vgs

  VG     #PV #LV #SN Attr   VSize   VFree   
  fedora   1   2   0 wz--n- 231.88g 212.96g

2) # vgdisplay

  --- Volume group ---
  VG Name               fedora
  System ID
  Format                lvm2
  Metadata Areas        1
  Metadata Sequence No  3
  VG Access             read/write
  VG Status             resizable
  MAX LV                0
  Cur LV                2
  Open LV               2
  Max PV                0
  Cur PV                1
  Act PV                1
  VG Size               231.88 GiB
  PE Size               4.00 MiB
  Total PE              59361
  Alloc PE / Size       4844 / 18.92 GiB
  Free  PE / Size       54517 / 212.96 GiB
  VG UUID               9htamV-DveQ-Jiht-Yfth-OZp7-XUDC-tWh5Lv

3) # lvextend -l +100%FREE /dev/mapper/fedora-root

  Size of logical volume fedora/root changed from 15.00 GiB (3840 extents) to 227.96 GiB (58357 extents).
  Logical volume fedora/root successfully resized.

4) #lvdisplay

5) #fd -h

6) # xfs_growfs /dev/mapper/fedora-root

meta-data=/dev/mapper/fedora-root isize=512    agcount=4, agsize=983040 blks
         =                       sectsz=512   attr=2, projid32bit=1
         =                       crc=1        finobt=1 spinodes=0 rmapbt=0
         =                       reflink=0
data     =                       bsize=4096   blocks=3932160, imaxpct=25
         =                       sunit=0      swidth=0 blks
naming   =version 2              bsize=4096   ascii-ci=0 ftype=1
log      =internal               bsize=4096   blocks=2560, version=2
         =                       sectsz=512   sunit=0 blks, lazy-count=1
realtime =none                   extsz=4096   blocks=0, rtextents=0
data blocks changed from 3932160 to 59757568


7) #df -h

Filesystem               Size  Used Avail Use% Mounted on
devtmpfs                 1.9G     0  1.9G   0% /dev
tmpfs                    1.9G     0  1.9G   0% /dev/shm
tmpfs                    1.9G  828K  1.9G   1% /run
tmpfs                    1.9G     0  1.9G   0% /sys/fs/cgroup
/dev/mapper/fedora-root  228G  2.3G  226G   2% /
tmpfs                    1.9G     0  1.9G   0% /tmp
/dev/md126p1             976M  119M  790M  14% /boot
tmpfs                    388M     0  388M   0% /run/user/0

Best regards,

1
  • It worked on my ubuntu VM!
    – omeraiman
    Mar 15, 2023 at 17:04
5

This is an old question, but I wanted to give my answer as well.

Since we're talking about free available space, we should talk about sectors, since no partitioning or sizing of sectors is done.

For us human beings this doesn't make much sense. To have human-readable information we must translate this number into bytes. So, we have a disk already partitioned and we want to know how much space we may use. I personally don't like the parted solution because my brain-memory for commands is already taken. There is also cfdisk, which gives you free space. But I think fdisk is the quickest solution: it's plain and simple, with nothing to install: execute fdisk /dev/sdx and then enter v into the interactive shell. It will gives you the number of sectors still free.

2004-54-0 [17:03:33][root@minimac:~]$> fdisk /dev/sda
Welcome to fdisk (util-linux 2.23.2).
..
Command (m for help): v
Remaining 1713 unallocated 512-byte sectors

We still have 1713 sectors at 512 bytes each. So, because you love terminal (in 2012, who knows now?) we do echo $(((1713*512)/1024))k, which is 1713 sectors multiplied for 512 bytes (divided by 1024 to have KB), which gives 856k.. not even 900 KB.. and I need another disk..

2
  • Thanks to @the Tin Man my answer has a better English grammar. One of the many languages I still have to improve ;)
    – nnsense
    Jan 15, 2017 at 11:36
  • How to know the start and end of the unallocated?
    – alhelal
    Jun 3, 2017 at 8:32
3

Just follow below.

  • find out the dev type, whether it is /dev/sda /dev/hda /dev/vda etc.

  • look for vi /etc/fstab and find out the mounted partisions and there UUIDs etc

  • say, your harddisk is labeled as /dev/sda and you know number of /dev/sda under df -hT

then you need to find out remaining /dev/sda* right.

so,

fdisk -l /dev/sda* will give the ALL /dev/sda* and you will find for example, /dev/sda4 or /dev/sda5

then find out UUIDs of mounted partisions and those are not listed in /etc/fstab are the ones you can format and mount.

just follow this up. a world to wise is sufficient.

3

While using the disk utility graphically, it shows disk space used by all filesystem and it uses commands in the terminal such as df -H. In other words, it uses powers of 1000, not 1024. (Note: there is difference between -h and -H.)

While also finding the unallocated space in a hard disk using command line # fdisk /dev/sda will display the total space and total cylinder value.

Now check the last cylinder value and subtract it from the total cylinder value. Hence the final value * 1000 gives you the unallocated disk space.

Note: the cylinder value shows up in df -H as a power of 1000 or it might also show up using df -h, a power of 1024.

1

A simple solution to the answer:

parted /dev/sda

Display the help on unit. Then toggle it to the units you want.

To show free space on the device, use:

print free
1
  • 1
    it will not going to print the free space.
    – ji-ruh
    Apr 11, 2018 at 3:42
0

If you need to see your partitions and/or filers with available space, mentioned utilities are what you need. You just need to use options.

For instance: df -h will print you those information in "human-readable" form. If you need information only about free space, you could use: df -h | awk '{print $1" "$4}'.

1
  • thanks for the reply. But what i'm asking is not the available space on the mounted disks. Let me explain it in more detail. I have 500 GB HD. 150GB is Win7 (C drive), 255 GB is Win7 (D drive). 65 GB is Fedora with all it's partitions like (/boot, /root, etc). now if you calculate, there is some space left on my HD where nothing is mounted, IOW no filesystem exist on that free space, neither linux nor windows/dos. that's why i used the term 'Unallocated Free space'
    – AniketGM
    Sep 7, 2012 at 19:19
0

You might want to use the fdisk -l /dev/sda command to see the partitioning of your sda disk. The "free space" should be some unused partition (or lack of).

3
  • 1
    I have used this command earlier sir, but the results show the same as i told before. i.e only mounted spaces. May be i think this is where linux terminals falls short. I searched lots of commands and used their many combinations, but no result.
    – AniketGM
    Sep 8, 2012 at 8:23
  • No, fdisk is showing disk partitions. If no free space is shown then you don't have any. Sep 8, 2012 at 9:44
  • sorry to be late. alright i'll show you the disk partitons shown by Disk Analyser. here's the link i48.tinypic.com/293j5f4.png
    – AniketGM
    Sep 13, 2012 at 18:41
0

The filesystem size can be different from the patition size. To repair you need to do this

df -h

see what is the name of the partition say /dev/sda3

resize2fs /dev/sda3

-1

when you cut you disk in partitions by fdisk you may be careful so as not to left gaps with free space. So command automatically align partitions and you'll get some gaps between parts. There are many articles in net why need to do so. The reason is that it gives solution with less errors. That was many years ago. Now I don't know is there errors occurs if you do all without any gaps. But first. You may do so if you don't allow alignment you sen begin of the next part=end previous+1. But is first part begins always with 2048 sector. So call expert part you may shift it to 0. But strongly recomended to do so if you plan to boot from this disk. If only for data you'll gain 1 Mb additional disk space. This is an MBR space. If you plan to install OS on this disk you don't use GPT partition type. Also it's more suitable not all OS see GPT parts of disks. But some see them. If you don't sure it use msdos. While format the block size is 4096 bytes(logical) physical one is 512 bytes. I don't do so but you may set block size=512 too. There was many discussion about that. It's lead to disk errors. But you'll give some free disk space too especially when you have many small size files. You disk will fill more compactly. And if you give already partitioned disk with filled them with data and maybe installed OS you maybe want to do so, it was very problems to do. But is possible for Linux. For Windows no... You must save backup and mbr too, write UUID every part then use fdisk and format as setting right UUID and LABEL for every part restore mbr with dd command and if you don't do any wrong all will be work as before but without any gaps.

1
  • 3
    Please format your answer into paragraphs, perhaps some bolding for the most relevant parts. Right now it's a long wall of text. Jul 20, 2017 at 11:06

Not the answer you're looking for? Browse other questions tagged or ask your own question.