Quantcast
Viewing latest article 20
Browse Latest Browse All 21

Answer by Oliver for A standard tool to convert a byte-count into human KiB MiB etc; like du, ls1

When you just want a quick conversion without fancy output, or when the numfmt is not available, I find the units handy. Eg define the following in your .bashrc:

convu() {   conv=$( units $2 $3 | grep / | cut -f 2 -d " " )  echo $(( $1 / $conv ))}

Then you can do this:

$ convu 268435456 bytes megabytes256

or this:

$ convu $( some command that outputs a value in bytes ) bytes megabytes256

Additionally if you define unit abbreviations in a file, say ~/.units.bytes then you can define convu instead as

convu() {   conv=$( units -f ~/.units.bytes $2 $3 | grep / | cut -f 2 -d " " )  echo $(( $1 / $conv ))}

If the file has

bit                     !h!nibble                  4 bitnybble                  nibblebyte                    8 bitword                    2 byteblock                   512 bytekbyte                   1024 bytemegabyte                1024 kbytegigabyte                1024 megabyteterabyte                1024 gigabytepetabyte                1024 terabyteexabyte                 1024 petabytezettabyte               1024 exabyteyottabyte               1024 zettabytekilobyte                kbytemeg                     megabytebaud                    bit/secb                       bytemb                      megabytegb                      gigabytekb                      kilobyte

then you can write

$ convu 268435456 b mb256

Viewing latest article 20
Browse Latest Browse All 21

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>