857

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

2 Answers 2

983

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.

16
  • 20
    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). Feb 8, 2013 at 12:59
  • 3
    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, 2013 at 13:15
  • 3
    @EmanuelBerg, 1000^7 (10^21) is greater than 2^64 (which is ~10^19.27) Feb 10, 2013 at 10:17
  • 8
    @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, 2017 at 14:56
  • 5
    @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. Feb 11, 2018 at 21:47
682

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

ls from the GNU coreutils package gives sizes in binary byte format in this case, e.g. Mebibyte (MiB), which is strongly endorsed by IEEE and CIPM instead of Megabyte (MB).

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.

10
  • 5
    That will print file sizes in GB for anything bigger than just under 1.0 GiB (I believe).
    – user
    Feb 8, 2013 at 8:36
  • 18
    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, 2017 at 10:08
  • 12
    "human-readable" is kind of ironic. Who uses ls -l at all if not humans? :p
    – phil294
    May 9, 2017 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. Feb 22, 2018 at 21:03
  • 2
    @Blauhirn: sometimes you want an exact size in bytes you can copy/paste or compare with another number, instead of a rounded size you can read quickly. But I see what you mean, it is a bit funny. Feb 22, 2018 at 21:16

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .