10

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.

dspjm
  • 5,473
  • 6
  • 41
  • 62
  • 1
    99% of the time it will be 1024, because that's what most computer scientists use. But if you want to be sure, read the documentation for the program in question, there's no way to know a priori. – Barmar Jul 26 '13 at 03:12

1 Answers1

16

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.

Daniel Widdis
  • 8,424
  • 13
  • 41
  • 63
  • 2
    I know it is quite long time since the initial answer, but personally I think Ubuntu policy got it wrong about file sizes or at least defaulting it to base-10. There are so many applications that use old windows format. I think the more effective way would have been to fix the presentation, but it seems to be now more confusing. As it comes to command line tools, most use the old format which back then was for some reason not so confusing, but now are thanks to Ubuntu policy for GUI applications. Probably since they are from pre-policy time & use base-2 with wrong notation. – Kris Avi Nov 16 '21 at 16:15