调用statvfs和df命令之间的差异

当我在Linux机器上使用statvfs命令获取已安装文件系统上的可用空间时,我得到的数字与df报告的数字略有不同。

例如,一个机器上我有一个500G硬盘驱动器,我从df获得以下输出:

# df --block-size=1 --no-sync Filesystem 1B-blocks Used Available Use% Mounted on /dev/md0 492256247808 3422584832 463828406272 1% / tmpfs 2025721856 0 2025721856 0% /lib/init/rw varrun 2025721856 114688 2025607168 1% /var/run varlock 2025721856 4096 2025717760 1% /var/lock udev 2025721856 147456 2025574400 1% /dev tmpfs 2025721856 94208 2025627648 1% /dev/shm 

对statvfs的调用给我一个4096和119344155空闲块的块大小,所以应该有488,833,658,880个字节空闲。 然而,DF报告有463,828,406,272字节免费。 为什么这里有差异?

由于您的差异接近5%[1],这是为root分配的默认百分比,您可能会将df结果与statvfs的 – > f_bfree进行比较,而不是使用 – > f_bavail,这是df的用途。

[1] 🙁 488833658880-463828406272)/ 492256247808 = 0.0508

 #include <stdio.h> #include <sys/statvfs.h> int main(){ struct statvfs stat; int a=statvfs("/",&stat); printf("total free disk space of the partition: %d GB \n",(stat.f_bavail)*8/2097152); //512 is 2^9 - one half of a kilobyte. //A kilobyte is 2^10. A megabyte is 2^20. A gigabyte is 2^30. A terabyte //is 2^40. And so on. The common computer units go up by 10's of powers //of 2 like that. //So you need to divide by 2^(30-9) == 2^21 == 2097152 to get gigabytes. //And multiply by 8 because 1 byte=8bit return 0; } 

我以这种形式做,因为我更喜欢Gb中的结果,但是你可以修改改变指数的单位。如你所见,第一个答案是真的,我也使用它