I am writing a c++ program that I need to get partitions and hhd's total, free and used space.
I'm so confused with all commands and codes that can be used to get these data from system and all differences between outputs.
I read this link : Why are there so many different ways to measure disk usage? , that was useful, but didn't really helped me to solve my problem
I understood that reason of difference in sizes between output of lsblk and df is that df gives file system's size and lsblk gives partition's size. the thing that I need is also partition's size. but as far as I know, lsblk only gives total size of partiton.(am I right?)
I also used statvfs structure in my code and outputs were so close to df's output.
so what am I supposed to use to get exact free, used and total size of partition and hdds?

  • What is the difference between "partition's size" and "total size of partition"? I'm not sure what you mean, please expand. It is very hard to give an exact size of free space available for data on a disk, since that depends on how you are going to use the remaining space: for example writing lots of small files versus one big file may result in different proportions of useful data in relation to the total amount used including metadata and internal file system structures. For a start, one file only needs space for a single file name, a million files needs space for a million names. Jul 21, 2020 at 7:42
  • there is no difference, I mean free, used and total sizes any way. actually I have been said to write a program to give those sizes of each partition. I searched a lot and saw different outputs of commands and codes that now I don't know which one suits correct answer to my problem. @JohanMyréen
    – fa7eme
    Jul 21, 2020 at 8:11

1 Answer 1


On the command line, you could use:


But in a c++ program, you generally don't want to parse the output of any other commands: you should use system and library calls to get the information directly.

Finding the right calls for the job is more Stack Overflow territory, but here are a few pointers:

  • actually I want to find exact command and see it's source code to know it's way of calculating space. I tested command you mentioned but it didn't have those option like FUSED and.. and about statvfs, as I mentioned, it gives output like df, and df is about file system's total and other things size. about the other code, it doesn't have permission to open /dev/sda. did i do something wrongly?
    – fa7eme
    Jul 21, 2020 at 8:03
  • The lsblk command I suggested works in Debian 10. lsblk --version says lsblk from util-linux 2.33.1. If your distribution has an older version, it might not have all the same options. If it has a newer version, you might want to check its man page and/or lsblk --help - the syntax may have changed. (And this is why you want to avoid parsing command output.) The source code for lsblk can be found here.
    – telcoM
    Jul 21, 2020 at 9:07

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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