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

up vote 392 down vote accepted

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.

  • 9
    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
  • 2
    @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. :)) – a CVn Feb 8 '13 at 13:34
  • 2
    @EmanuelBerg, 1000^7 (10^21) is greater than 2^64 (which is ~10^19.27) – Stéphane Chazelas Feb 10 '13 at 10:17
  • 4
    @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. – a CVn Dec 9 '17 at 14:56

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). – a CVn Feb 8 '13 at 8:36
  • 5
    @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
  • 5
    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
  • 6
    "human-readable" is kind of ironic. Who uses ls -l at all if not humans? :p – Blauhirn May 9 '17 at 14:17
  • 2
    @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 at 21:03

ls -lhS sort by size, in human readable format

  • 1
    Please see the comments on ls -lh answer for reasons why this doesn't really answer the question. – Alexander Batischev May 4 '16 at 19:11
  • 1
    Not only does this not really answer the question (as discussed in the comments to Sirex's answer) but it also doesn't add anything relevant over either existing answer: the OP isn't asking about sort order. – a CVn May 30 '16 at 12:41
  • 4
    just thought sorting by size is a potential follow-up people might have, at least that's what I had – jaciefan Jun 3 '16 at 21:53
  • In that case people should be looking at How to list all files ordered by size which is about exactly that scenario. – a CVn Oct 28 '16 at 14:04

And this is the option for listing everything in a directory order by file size (still human readable) ascending.

ls -lSrh

protected by Community May 12 at 15:38

Thank you for your interest in this question. Because it has attracted low-quality or spam answers that had to be removed, posting an answer now requires 10 reputation on this site (the association bonus does not count).

Would you like to answer one of these unanswered questions instead?

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