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


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.

  • 15
    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). – Stéphane Chazelas Feb 8 '13 at 12:59
  • 2
    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 Feb 8 '13 at 13:15
  • 3
    @EmanuelBerg, 1000^7 (10^21) is greater than 2^64 (which is ~10^19.27) – Stéphane Chazelas Feb 10 '13 at 10:17
  • 7
    @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. – user Dec 9 '17 at 14:56
  • 3
    @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. – John Madden Feb 11 '18 at 21:47

ls -lh gives human readable file sizes, long format.

It uses k, M, G, and T suffixes (or no suffix for bytes) as needed so the number stays small, e.g. 1.4K or 178M.

-h is a GNU coreutils extension, not baseline POSIX.

Note that this doesn't answer the question exactly as asked. If you want sizes strictly in MiB even for small or gigantic files, Michael Kjörling's answer does that for GNU coreutils ls.

  • 4
    That will print file sizes in GB for anything bigger than just under 1.0 GiB (I believe). – user Feb 8 '13 at 8:36
  • 7
    @MichaelKjörling I don't see it as a bad answer (although I didn't upvote). Sometimes the OP doesn't word a question exactly as they should so it was possible that they just wanted human readable output and instead phrased that as "in MB". – Dason Feb 8 '13 at 14:33
  • 12
    Upvoted. "This doesn't answer the (MB) question" is a perfectly valid statement but this answer has only increased the usefulness of this SO page since this answer is in same context. I only came to this page looking for a solution to generic "show dates in human readable form" requirement and h is far easier to write than --block-size=M – Ejaz Mar 19 '17 at 10:08
  • 9
    "human-readable" is kind of ironic. Who uses ls -l at all if not humans? :p – phil294 May 9 '17 at 14:17
  • 3
    @chengyang: parsing ls -l output is the wrong way to do things, 99% of the time. unix.stackexchange.com/questions/128985/why-not-parse-ls. That 1% is being generous, and is pretty much limited to one-liners you write interactively for one-time use, not a script you're going to use repeatedly on unknown filenames. – Peter Cordes Feb 22 '18 at 21:03

Not the answer you're looking for? Browse other questions tagged or ask your own question.