How to find the size of file in MB in UNIX command line?
-
1This 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
-
14@Robert, very wrong. Shell scripting is a vital part of any sysadmin's job. – churnd Jul 7 '09 at 11:02
-
1Churnd 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
If your ls
supports --block-size
such as GNU coreutils ls
does:
ls -s --block-size=1048576 filename | cut -d' ' -f1
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.
-
1Interestingly, I havw a
120672256
byte file that shows116M
withdu
and,115M
with thels
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 withls
). – 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 thels
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
-
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.
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)}'
-
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)
-
1
-
-
1stat 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
I found an AWK 1 liner, and it had a bug but I fixed it. I also added in PetaBytes after TeraBytes.
FILE_SIZE=234234 # FILESIZE IN BYTES
FILE_SIZE=$(echo "${FILE_SIZE}" | awk '{ split( "B KB MB GB TB PB" , v ); s=1; while( $1>1024 ){ $1/=1024; s++ } printf "%.2f %s", $1, v[s] }')
Considering stat is not on every single system, you can almost always use the AWK solution. Example; the Raspberry Pi does not have stat but it does have awk.
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
I saw this on another thread and liked the results. Under AIX, I used
ls -l [filename] | awk '{$5=sprintf("%.3f GB", $5/1024^3)} 1'
Produces the GB count with 3 decimal places
Example output of
ls -l /tmp/myfile:
-rw-rw-rw- 1 owner group 0.530 GB Jul 8 10:33 /tmp/myfile
You may opt to increase the decimal count if the file is smaller than 1 MB. The example above, using %.9f instead would give:
0.530388314 GB
Try with this example:
size=$(du file | awk '{print $1/1000}')
echo "the size is ${size} MB"