Search code examples
unixsizelshuman-readable

How does `-h` in `ls -lah` work in Unix?


When I use ls -la and ls -lah, I seem to get different number for the same files.

gillin@mt:/home/vanilla/wmt-data/lm$ ls -lah *
-rw-rw-r-- 1 vanilla vanilla 68G Aug  5 03:57 lm.6gram.de.arpa.gz
-rw-rw-r-- 1 vanilla vanilla 25G Aug  4 07:32 lm.6gram.fr.arpa.gz
-rw-rw-r-- 1 vanilla vanilla 29G Aug  5 07:41 lm.6gram.ru.arpa.gz
gillin@mt:/home/vanilla/wmt-data/lm$ ls -la *
-rw-rw-r-- 1 vanilla vanilla 72726733721 Aug  5 03:57 lm.6gram.de.arpa.gz
-rw-rw-r-- 1 vanilla vanilla 26482284407 Aug  4 07:32 lm.6gram.fr.arpa.gz
-rw-rw-r-- 1 vanilla vanilla 30777650366 Aug  5 07:41 lm.6gram.ru.arpa.gz

Hows does the -h parameter round the size of the file?

Why is the -lah size so different from the -la


Solution

  • They're not "so different". -h is "human readable":

    30777650366 -> 30056299K -> 29351M -> 28G for powers of 2
    30777650366 -> 30777650K -> 30056M -> 30G for powers of 10
    

    If you'd RTFM'd (man ls):

      -h, --human-readable
             with -l and/or -s, print human readable sizes (e.g., 1K 234M 2G)
    
      --si   likewise, but use powers of 1000 not 1024