ls-command: how to display the file size in megabytes?

In Unix (Tru64), how do I make the ls command show the file size in megabytes? Currently I am able to show it in bytes using the following:

ls -la

Solution 1:

Maybe -h is sufficient for you:

-h
When used with the -l option, use unit suffixes: Byte, Kilobyte, Megabyte, Gigabyte, Terabyte and Petabyte in order to reduce the number of digits to three or less using base 2 for sizes.

ls -lah

General advice: Use man commandname to read the manual/help of a certain command, e.g. here man ls.

Solution 2:

ls --block-size=M prints the sizes in Megabytes but shows 1MB also for anything below 1 MB. I'm unsure if this option is acceptable in your UNIX version of ls, though.

Actually ls -lh also prints sizes in Gigabytes if the file is big enough (Well anyways: on Linux 64bit this does work :>)

On a side node: du -sh * prints also directory sizes in current directory.

Solution 3:

You will have to use awk to do the math for you:

ls -l | awk 'BEGIN{mega=1048576} $5 >= mega {$5 = $5/mega "MB"} {print}'

This won't affect the output for files that are smaller than mega.

You may need to adjust the field number to match the way your ls is laid out. You can change mega to "1000000" if that is your preference.

This will print more decimal places than you probably want. You could implement a rounding function.

Solution 4:

try ls -shR for recursive human readable format.

Solution 5:

try ls -lash, it prints sizes in human readable format