4

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 1

2

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
  • OK, thank you very much, I have read the source code and find the calculation: pct = u100 / nonroot_total + (u100 % nonroot_total != 0);
    – Tanky Woo
    Jan 27, 2014 at 16:07

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .