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
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
.
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.
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
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.
ls -lh
or ls --block-size=M
, but AWK is very useful!
try ls -shR
for recursive human readable format.
-h
on Tru64, and did so over two years before this answer was posted.
try ls -lash
, it prints sizes in human readable format
du -sm filename.txt
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.
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}'
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.
-h
option does not exist in ls
on that system.
Commented
Jun 13, 2015 at 8:14
ls -l --block-size=MB
For the --block-size
parameter:
MB
for 10^6M
for 2^201MB
. For this reason I found ls -l --block-size=kB
to be more useful.
Commented
Oct 26, 2015 at 2:31
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.