170

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
0

10 Answers 10

272

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.

9
  • 1
    Thanks, but -h option does not seem to exist on Tru64 Unix.
    – atricapilla
    Sep 20, 2010 at 11:26
  • 8
    General advice: install GNU utilities ... Sep 20, 2010 at 12:35
  • 23
    I like your tip about using man, but really, searching on Google is so much faster than using man a lot of the time (as now, when I found this result on Google).
    – trusktr
    Mar 20, 2013 at 5:00
  • 5
    Google's fine and all for general hints, but ultimately man pages are system/OS, even host specific, usually created when the software is compiled or installed, and thus, authoritative for the software on your particular system. May 5, 2013 at 4:33
  • 1
    Just to come full circle, there's man online for gnu/linux: linux.die.net/man Oct 11, 2013 at 8:20
34

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.

1
  • Thanks! I needed to monitor live file changes in a folder and using ls -h is meaningless after a file grows over 1GB, so I use this command in a 1 second loop: while true ; do ls -al --block-size=M ; sleep 1 ; done
    – ccpizza
    Mar 19, 2016 at 13:13
17

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.

3
  • 3
    Much less elegant than ls -lh or ls --block-size=M, but AWK is very useful!
    – Statwonk
    Sep 28, 2014 at 14:50
  • 2
    Just what I needed. All the other solutions round to the nearest MB but this shows a few decimal places and is easily customizable.
    – zeroimpl
    Oct 17, 2014 at 0:02
  • 3
    This is a nice solution for those systems (AIX - I am looking at you) that do not have the -h option.
    – Buggabill
    Nov 18, 2015 at 13:22
13

try ls -shR for recursive human readable format.

2
  • 4
    OP has stated that there is no -h on Tru64, and did so over two years before this answer was posted.
    – user
    Oct 11, 2013 at 9:02
  • 3
    Thought It's my responsibility to help those who reach here with the same google search.
    – Sorter
    Feb 22, 2017 at 17:52
9

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

1
  • I kept forgetting this, but now I won't. "human memorizable format" :) Apr 7, 2020 at 11:10
5

du -sm filename.txt

1
  • 1
    it always round up the size...if size is less than 1 MB say 500K, then also it will print 1M
    – AnonGeek
    Jun 29, 2012 at 13:08
5

You can also type

du -sh ./*

This will list all the folders under current directory, with human-readable format, including the more familiar file sizes in Kb, Mb, Gb.

1
  • In that reply was posted a du implementation, that can show sizes in human readable format with 3 decimals: du -Lsbc * | awk 'function hr(bytes) {hum[1024**4]="TiB";hum[1024**3]="GiB";hum[1024**2]="MiB";hum[1024]="kiB";for (x = 1024**4; x >= 1024; x /= 1024) {if (bytes >= x) {return sprintf("%8.3f %s", bytes/x, hum[x]);}}return sprintf("%4d B", bytes);}{print hr($1) "\t" $2}'
    – Kyo
    Dec 24, 2022 at 14:17
2

If you just want the size of only a specific file, then the command, a trivial extrapolation of the previous answers, is:

ls -sh filename(s)

-s is for size and the h is for Human Readable (as mentioned above a few times).

The output will look like this:

753M myfilename

If you leave out the filename(s), it'll list the directory, placing the size of each file next to its name — not unlike what ls -la does when invoked with no filename arguments.

Hope this helps.

1
  • 2
    This question is about Tru64 Unix, and the OP stated almost five years ago that the -h option does not exist in ls on that system. Jun 13, 2015 at 8:14
2
ls -l --block-size=MB 

For the --block-size parameter:

  • use MB for 10^6
  • use just M for 2^20
1
  • Note that this won't show any decimal places. Also, anything above 0 kB but nor greater than 1 MB will be shown as 1MB. For this reason I found ls -l --block-size=kB to be more useful.
    – DaAwesomeP
    Oct 26, 2015 at 2:31
0

If you're just interested in the file size, and you don't have to use the ls command, try the following:

# echo "Hello World" > file.txt
# ls -l file.txt 
-rw-r--r-- 1 user user 12 Mar 10 11:32 file.txt
# stat --printf='%s\n' file.txt
12

This will print the file size without the need to parse anything.

2
  • OP want size in MB, maybe a stat option will produce size in human readable form ?
    – Archemar
    Mar 12, 2016 at 10:47
  • Ah, that was dumb of me. Thanks for catching that. stat does not have an option to manipulate the size using --printf or any other option as far as I know. It would likely require using a combination of printf and bc command-line utilities. In which case, why bother. ls and cut/awk would be quicker.
    – SKN
    Apr 23, 2016 at 21:42

You must log in to answer this question.