Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Convert between byte count and "human-readable" string

Tags:

shell

Is there a shell command that simply converts back and forth between a number string in bytes and the "human-readable" number string offered by some commands via the -h option?

To clarify the question: ls -l without the -h option (some output supressed)

> ls -l 
  163564736 file1.bin
      13209 file2.bin

gives the size in bytes, while with the -hoption (some output supressed)

> ls -lh 
  156M file1.bin
   13K file2.bin

the size is human readable in kilobytes and megabytes.

Is there a shell command that simply turns 163564736into 156M and 13209 into 13K and also does the reverse?

like image 871
Svaberg Avatar asked May 03 '16 21:05

Svaberg


People also ask

Can we convert byte to string?

One method is to create a string variable and then append the byte value to the string variable with the help of + operator. This will directly convert the byte value to a string and add it in the string variable. The simplest way to do so is using valueOf() method of String class in java.


1 Answers

numfmt

To:

echo "163564736" | numfmt --to=iec

From:

echo "156M" | numfmt --from=iec
like image 158
Trevor Hickey Avatar answered Sep 18 '22 19:09

Trevor Hickey