1

I'm using the following to get human friendly file sizes from the shell.

ls -hl /path/to/file | awk '{print $5}'

It works well in Mac OSX with bash. I'm not sure if its truly portable, or if there is a standard portable way to achieve the same result.

Haleemur Ali
  • 26,718
  • 5
  • 61
  • 85

2 Answers2

1

If you really want strict POSIX compatibility then you must do as is suggested in this answer to a very similar question, and do your own computation (e.g. in awk too) to calculate the desired "human-friendly" value.

Community
  • 1
  • 1
Greg A. Woods
  • 2,663
  • 29
  • 26
1

Most implementations of du(1) accept the -h flag, which causes it to print the disk usage of the file, or files, specified. The difference between disk usage and file size in normally innocuous, rounding up to the nearest file-system block.

The -h flag, though widely supported, is an extension. For maximum portability unit conversion would have to be done manually an pointed out by chepner.

*BSD, Illumos, BusyBox, and GNU CoreUtils all support the -h flag.

kdhp
  • 2,096
  • 14
  • 15