Take the 2-minute tour ×
Server Fault is a question and answer site for professional system and network administrators. It's 100% free, no registration required.

How to find the size of file in MB in UNIX command line?

share|improve this question
    
This question does not belong on serverfault. Shell scripting is not necessarily 'server stuff'. Or shall we say that those who code in scripting languages are not programmers? Hm... –  Robert Munteanu Jul 7 '09 at 10:08
10  
@Robert, very wrong. Shell scripting is a vital part of any sysadmin's job. –  churnd Jul 7 '09 at 11:02
    
Churnd is right. Command line is not necessarily shell scripting, ascertaining the status of a file system IS part of a SysAdmin's job, and shell scripting (JCL, Batch, whatever) IS part of a SysAdmin's job. This question DOES belong on ServerFault. –  kmarsh Jul 7 '09 at 12:33

6 Answers 6

If your ls supports --block-size such as GNU coreutils ls does:

ls -s --block-size=1048576 filename | cut -d' ' -f1
share|improve this answer

I tend to use 'du -k myfile' to get kbytes and visually drop the last three digits, but I'm just looking for approximate size.

Turns out that du often (always?) has -m option for MB.

Keep in mind that how large the file likely differs slightly from the amount of diskspace used, as the disk allocation occurs in blocks, not bytes.

If you are looking for 'fat' files because of low diskspace, that would be a more enlightening question, as the solutions would be more varied.

share|improve this answer

du -h file

OR

ls -lh file

EDIT

this answer is wrong since it can report size also in Gb/Kb, depending on the file's size. Please remove upvotes.

share|improve this answer
1  
Interestingly, I havw a 120672256 byte file that shows 116M with du and, 115M with the ls command... –  nik Jul 7 '09 at 9:51
    
Note: I am not at all suggesting these answers are incorrect. I would have done du -sh myself (which is what i did to check the difference with ls). –  nik Jul 7 '09 at 9:53
    
Ok, I think i have an answer for my question. The du counts disk space utilization for the file and the ls just counts the size of the file. –  nik Jul 7 '09 at 9:55
    
du -h should report the real size (allocated as multiple of your block size) –  dfa Jul 7 '09 at 9:56
    
@dfa, Right. I figured. thanks. –  nik Jul 7 '09 at 10:01

using -lh option will give you sizes in human readable form, e.g if your file is of size 1025 M it will output 1G, otherwise you can use ls --block-size=1024K -s it will give size in nearest integer in MBs.
If you need precise values (not rounded ones) you can go for awk:

ls -l | awk '/d|-/{printf("%.3f %s\n",$5/(1024*1024),$9)}'
share|improve this answer
    
you are right, my answer is wrong since it will report size in Gb/Mb/Kb –  dfa Jul 7 '09 at 10:04
    
Nitpick: 1024*1024 uses MiB (=2^20) instead of the requested MB (10^6). See also: en.wikipedia.org/wiki/Mebibyte –  Niels Basjes Jul 7 '09 at 11:18

stat can do this.

$ ls -l | grep myfile
-rw-------  1 rory rory      3120 2009-07-02 16:58 myfile
$ stat -c '%s' myfile
3120

that give it to you in bytes.

You can use bash's arithmetic to calculate the megabytes:

$ echo $(( $( stat -c '%s' myfile ) / 1024 / 1024 ))
0

(but it rounds it down in this case)

share|improve this answer
1  
Useless use of grep. Just do ls -l myfile. –  Dennis Williamson Jul 7 '09 at 13:43
    
@Dennis Williamson: Habit :P –  Rory Jul 7 '09 at 14:04
1  
stat is not on every flavor of *nix (or at least not installed by default). When it's there, it's nice however. –  ericslaw Jul 7 '09 at 14:24

You can use a tiny Python script:

$ cat ./size_in_mb.py
#!/usr/local/bin/python
import os
import sys
print os.path.getsize(sys.argv[1])/1048576

$ ls -l test.tgz
-rw-r--r--  1 root root 258330336 Jul  7 00:04 test.tgz

$ ./size_in_mb.py test.tgz
246
share|improve this answer

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.