How do I make `ls` show file sizes in megabytes?
ls -l --block-size=Mwill 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
If you don't want the
Msuffix 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
-lhto 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
1.5Gand so on.
--block-sizeparameter is described in the man page for ls;
man lsand 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
-hare 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
lsfrom 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).
@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?
@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).
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
@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.