How do I make `ls` show file sizes in megabytes?

  • What commands do I need for Linux's ls to show the file size in MB?

  • a CVn

    a CVn Correct answer

    8 years ago

    ls -l --block-size=M will give you a long format listing (needed to actually see the file size) and round file sizes up to the nearest MiB.

    If you want MB (10^6 bytes) rather than MiB (2^20 bytes) units, use --block-size=MB instead.

    If you don't want the M suffix attached to the file size, you can use something like --block-size=1M. Thanks Stéphane Chazelas for suggesting this.

    If you simply want file sizes in "reasonable" units, rather than specifically megabytes, then you can use -lh to get a long format listing and human readable file size presentation. This will use units of file size to keep file sizes presented with about 1-3 digits (so you'll see file sizes like 6.1K, 151K, 7.1M, 15M, 1.5G and so on.

    The --block-size parameter is described in the man page for ls; man ls and search for SIZE. It allows for units other than MB/MiB as well, and from the looks of it (I didn't try that) arbitrary block sizes as well (so you could see the file size as a number of 429-byte blocks if you want to).

    Note that both --block-size and -h are GNU extensions on top of the Open Group's ls, so this may not work if you don't have a GNU userland (which most Linux installations do). The ls from GNU Coreutils 8.5 does support --block-size and -h as described above. Thanks to kojiro for pointing this out.

    A difference maybe worth noticing: `--block-size=M` cause a `M` suffix to be displayed next to the size, and you can use `--block-size=1M` to omit it. It may be worth mentioning as well that you need GNU ls for that (most non-embedded Linux systems will have GNU ls).

    Would this be GNU `ls`? Standard `ls` has no such argument. With the xsi extension `ls` has the `-s` flag, which makes it report the number of blocks, but there is no standard flag `--block-size`.

    @kojiro GNU ls does have `--block-size` as of coreutils 8.5, which is what's installed on my Debian Squeeze system. Since the question was explicitly for Linux and did not specify anything else, I'll admit I assumed a userland centered around GNU coreutils. I have updated the answer to clarify this. (Besides, Open Group ls doesn't seem to have -h either. `:)`)

    @StephaneChazelas: `ls -l --block-size=Z` (as well as `ZB`, `Y`, and `YB`) gives the following error (for the corresponding `--block-size` argument): `ls: --block-size argument `Z' too large`. If I read `ls(1)` correctly, `Z` is 1000^7, `ZB` is 1024^7, `Y` is 1000^8, and `YB` is 1024^8. I have no intuition on such large numbers -- what do you think, is there an overflow on the application level (i.e., some C variable) or is it beyond the system architecture?

    @EmanuelBerg, 1000^7 (10^21) is greater than 2^64 (which is ~10^19.27)

    Typically when I'm looking at file sizes, am I looking at MiB or MB?

    @chris_anderson I don't know. Maybe you should ask that as a separate question.

    This returns `ls: illegal option -- -` for me on OSX Sierra 10.12.6

    @Tom As it says in the final paragraph of the answer, `--block-size` is a GNU extension. I suspect that Mac OS X doesn't use GNU `ls`.

    @MichaelKjörling I can confirm that the --block-size flag causes an error on Mac OS X 10.13.2, but that ls -lh does work.

    `du -sm *` is another (faster to type) way to get something similar. But `-m` isn't POSIX-standard either. (`-k` is, so you can `du -sk *` even on a system with only POSIX options).

    @PeterCordes Sure, but OP asked specifically for a solution using `ls`.

    Also, AFAIU, `ls -s --block-size=K` shows the file size rounded up *to the nearest block* (*disk size*), whereas `ls -lh` does not, and shows the factual *contents* size (for example, 396 KB vs 393 KB)

    For macOS, we have the GNU `coreutils` package with Homebrew and the command `gls` (prefixed with `g` to avoid conflicts). Works like a charm

    Note that this won't work at all on a thinly provisioned VM.

    @AlexJohnson It should work fine if your VM has a GNU userland, as already stated in the answer.

    Why is this long-winded, impossible to recall, overly verbose switch preferable to the simple, consistent and widely used across commands "human" readable switch: -h

    @RickO'Shea Because passing `-h` to `ls` doesn't guarantee that sizes are printed in megabytes. Since the question specifically asked how to make `ls` show file sizes in megabytes, suggesting to use `-h` provides an answer to the question only for a subset of the possibilities. That said, there's an answer specifically suggesting `ls -lh` if you prefer that.

License under CC-BY-SA with attribution


Content dated before 6/26/2020 9:53 AM