Take the 2-minute tour ×
Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems.. It's 100% free, no registration required.

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

share|improve this question
add comment

2 Answers

up vote 22 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 @StephaneChazelas for suggesting this.

This 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 number of 412-byte blocks, if you want to).

Note that the --block-size parameter is a GNU extension 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 as described above. Thanks to @kojiro for pointing this out.

share|improve this answer
3  
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). –  Stephane 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
1  
@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. :)) –  Michael Kjörling Feb 8 '13 at 13:34
1  
@EmanuelBerg, 1000^7 (10^21) is greater than 2^64 (which is ~10^19.27) –  Stephane Chazelas Feb 10 '13 at 10:17
1  
I guess we'll be moving to 128-bit architectures soon, so that --block-size=Z will work for ls. –  Michael Kjörling Feb 10 '13 at 12:38
show 2 more comments

ls -lh human readable file sizes, long format

share|improve this answer
2  
That will print file sizes in GB for anything bigger than just under 1.0 GiB (I believe). –  Michael Kjörling Feb 8 '13 at 8:36
    
yeah, sorry, i thought that's what was wanted (as opposed to bytes). –  Sirex Feb 8 '13 at 8:48
1  
I must say I'm a little surprised that this has received two upvotes, considering that it doesn't answer the question. (Or rather, it only does so when file sizes are in the range 1 MiB - 1 GiB.) -h is a useful switch that I too use quite often, but it doesn't do what the OP is after. –  Michael Kjörling Feb 8 '13 at 10:35
    
@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
    
@Dason I didn't downvote this, either, for precisely the reason you state as well as the fact that for most semi-large files, -h does meet the OP's stated requirement quite nicely. But I also didn't upvote, because as I said it doesn't really answer the question. As for "phrasing it wrong", well, the OP has been on SE (ServerFault, StackOverflow and SuperUser) at least since late 2009 - early 2010, so certainly can hardly be considered a newcomer to the site. –  Michael Kjörling Feb 8 '13 at 14:39
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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