Unix & Linux Asked by fa7eme on October 31, 2021
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?
On the command line, you could use:
lsblk -bo NAME,SIZE,FSTYPE,FSSIZE,FSUSED,FSAVAIL,FSUSE%
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:
statvfs(2)
or std::filesystem::space
.BLKGETSIZE64
ioctl to get the size in bytes, and if you need the size in blocks, also use the BLKSSZGET
ioctl to get the block size, and divide the size in bytes by the size of a single block. Here's another example.Answered by telcoM on October 31, 2021
Get help from others!
Recent Answers
Recent Questions
© 2024 TransWikia.com. All rights reserved. Sites we Love: PCI Database, UKBizDB, Menu Kuliner, Sharing RPP