'Convert KB To MB using Bash

I use a command to get the size of a remote folder, after it's run it returns

120928312 http://blah.com

The number is size in bytes. What I'd like to do is have it output in MB, and the http part removed. I'm guessing greping to a file but not sure how to go about it.



Solution 1:[1]

You can do it with shell builtins

some_command | while read KB dummy;do echo $((KB/1024))MB;done

Here is a more useful version:

#!/bin/sh
human_print(){
while read B dummy; do
  [ $B -lt 1024 ] && echo ${B} bytes && break
  KB=$(((B+512)/1024))
  [ $KB -lt 1024 ] && echo ${KB} kilobytes && break
  MB=$(((KB+512)/1024))
  [ $MB -lt 1024 ] && echo ${MB} megabytes && break
  GB=$(((MB+512)/1024))
  [ $GB -lt 1024 ] && echo ${GB} gigabytes && break
  echo $(((GB+512)/1024)) terabytes
done
}

echo 120928312 http://blah.com | human_print

Solution 2:[2]

How about this line:

$ echo "120928312 http://blah.com" | awk '{$1/=1024;printf "%.2fMB\n",$1}'
118094.05MB

Solution 3:[3]

Try doing this using builtins (display an integer like the KB version)

var="120928312 http://blah.com"
echo "$(( ${var%% *} / 1024)) MB"

Solution 4:[4]

function bytes_for_humans {
    local -i bytes=$1;
    if [[ $bytes -lt 1024 ]]; then
        echo "${bytes}B"
    elif [[ $bytes -lt 1048576 ]]; then
        echo "$(( (bytes + 1023)/1024 ))KiB"
    else
        echo "$(( (bytes + 1048575)/1048576 ))MiB"
    fi
}

$ bytes_for_humans 1
1 Bytes
$ bytes_for_humans 1024
1KiB
$ bytes_for_humans 16777216
16MiB

Solution 5:[5]

Using bc and printf

bc and printf can be used to show the output with a configurable number of decimal places, also grouping digits:

$ KB=1234567890
$ echo "$KB / 1000" | bc -l | xargs -i printf "%'.1f MB" {}
1,234,567.9 MB

Using numfmt

To use numfmt to auto-scale the output unit:

$ numfmt --from=iec --to=si 123456789K
127G
$ numfmt --from=si --to=iec 123456789K
115G

To use numfmt to output to a fixed unit, e.g. M:

$ numfmt --from=iec --to-unit=1M --grouping 123456789K
126,420

$ numfmt --from=si --to-unit=1Mi --grouping 123456789K
117,738

See its man page and ensure it is used correctly.

Solution 6:[6]

Try using awk

awk '{MB=$1/1024; print $MB}'

$1 - value of the first column, size (KB) in this case

Solution 7:[7]

Github: index0-b-to-mb.sh

index0-b-to-mb.sh

    #! /bin/bash --posix

    # requirements: vim-common
    # sudo dnf -y install vim-common

    function b_to_mb {
      # get BASE conversion type
      if [ "$3" = "BASE10" ]; then
        # set for BASE10
        BASE_DIV=1000000
      else
          if [ "$3" = "MIXED" ]; then
          # set for MIXED
          BASE_DIV=1024000
          else
          # set default for BASE2
          BASE_DIV=1048576
          fi
      fi
      # create array from string
      # use bc with 6 digit precision to calculate megabytes from bytes
      ARRAY=($1) && printf "scale=6; ${ARRAY[0]}/$BASE_DIV\n" | bc -l
    }
    # execute b_to_mb
    b_to_mb $1 $2

https://en.wikipedia.org/wiki/Megabyte#Definitions

./index0-b-to-mb.sh '120928312 http://blah.com' MIXED

118.094054

./index0-b-to-mb.sh '120928312 http://blah.com' BASE10

120.928312

./index0-b-to-mb.sh '120928312 http://blah.com' BASE2

115.326225

./index0-b-to-mb.sh '120928312 http://blah.com'

115.326225

./index0-b-to-mb.sh "$(your_command)"

115.326225

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Asclepius
Solution 2 Asclepius
Solution 3
Solution 4
Solution 5
Solution 6
Solution 7 Stef