M HYPE SPLASH
// general

what is 1K-blocks in df, and how to calculate Use percentage?

By Sarah Scott

I have search many and not found 1K-blocks meaning in df command(gnu), but I have calculated and think it equals to 1K Byte? Is there a offical explanation?

Then how to calculate the Used Percentage?

For example:

tankywoo@gentoo-jl::~/ » df -h
Filesystem Size Used Avail Use% Mounted on
/dev/sda3 15G 5.9G 8.2G 42% /
tankywoo@gentoo-jl::~/ » df
Filesystem 1K-blocks Used Available Use% Mounted on
/dev/sda3 15481840 6163320 8532088 42% /

In my local machine, I know there is reserved space.

Used is 6163320, Avail is 8532088, so:

I think Used% should be (15481840-8532088)/15481740 = 44.88%, not 42%.

So how to get the result 42%?

1 Answer

The 1K block in GNU coreutils df(1) means 1024 bytes. Confirmed by taking a quick look at GNU coreutils, version 8.13, source code:

964 if (human_output_opts == -1)
965 {
966 if (posix_format)
967 {
968 human_output_opts = 0;
969 output_block_size = (getenv ("POSIXLY_CORRECT") ? 512 : 1024);
970 }
971 else
972 human_options (getenv ("DF_BLOCK_SIZE"),
973 &human_output_opts, &output_block_size);
974 }

As you can see, default output block size is 1024, unless environment variable POSIXLY_CORRECT is set.

When calculating used percentage, df(1) subtracts reserved space/blocks for root user from the available space, when underlying filesystem supports reserved space/blocks (most do):

529 if (known_value (total) && known_value (available_to_root))
530 {
531 used = total - available_to_root;
532 negate_used = (total < available_to_root);
533 }

To sum this up, the official authority in this and every case is the source code.

1

Your Answer

Sign up or log in

Sign up using Google Sign up using Facebook Sign up using Email and Password

Post as a guest

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