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
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
1.5G and so on.
--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
-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.
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.
-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
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?