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)}'
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.
If your
ls
supports--block-size
such as GNU coreutilsls
does: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.
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:
stat
can do this.that give it to you in bytes.
You can use bash's arithmetic to calculate the megabytes:
(but it rounds it down in this case)
I found an AWK 1 liner, and it had a bug but I fixed it. I also added in PetaBytes after TeraBytes.
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:
I saw this on another thread and liked the results. Under AIX, I used
Produces the GB count with 3 decimal places
Example output of
You may opt to increase the decimal count if the file is smaller than 1 MB. The example above, using %.9f instead would give:
Try with this example: