dspjm
dspjm

Reputation: 5860

How do we determine whether a size is base on 1000 or 1024 when we see kb or mb

There are many programs in Linux which would show the size of a file, some of them show it in blocks, some are in bytes. But when it comes to some human readable form, like ls -sh, lvs, dd bs=size and so many, how do we decide if it's a multiple of 1024 or 1000 when we see a kb, KB, mB, MB, K, G etc. Some distinguish them with capitalization like lvs, some with different characters like dd, however, is there a general rule of these kinds of things cause I can't find it so far. Thanks.

Upvotes: 12

Views: 8136

Answers (1)

Daniel Widdis
Daniel Widdis

Reputation: 9140

If you look at man units, you'll see a description of the two types of units. Decimal and Binary. Decimal units like Kilobyte (KB) and Megabyte (MB) are in multiples of 1000 (10^3) while Binary units like Kibibyte (KiB) and Mebibyte (MiB) are in multiples of 1024 (2^10).

If the unit being displayed includes a binary prefix like KiB, MiB, GiB, you can be certain it's 1024. For unclear units, a general thumb rule:

  • Hard Drive sizes are advertised in Decimal units, because the manufacturers like to make them look bigger. Accordingly, the size of files stored on the disk and transferred over networks are typically consistent with this.
  • Memory sizes are advertised in Binary units
  • Anything not data related (frequency in KHz, etc.) is always Decimal

Ubuntu published a policy in 2010 for their units which appears to be reasonably consistent across Linux distributions, although not guaranteed:

  • Use base-10 for:

    • network bandwidth (for example, 6 Mbit/s or 50 kB/s)
    • disk sizes (for example, 500 GB hard drive or 4.7 GB DVD)
  • Use base-2 for:

    • RAM sizes (for example, 2 GiB RAM)

For file sizes there are two possibilities:

  • Show both, base-10 and base-2 (in this order). An example is the Linux kernel: "2930277168 512-byte hardware sectors: (1.50 TB/1.36 TiB)"
  • Only show base-10, or give the user the opportunity to decide between base-10 and base-2 (the default must be base-10).1

1 As noted by Kris Avi in a comment, some command-line tools developed before this policy may use only base-2 values but indicate decimal units, and may not have changed in order to avoid breaking existing parsing scripts.

Upvotes: 20

Related Questions