File size in human format

Given the file size in bytes, I want to format it with IEC prefixes (binary) to 3 significant digits with trailing zeros , for example, 1883954 becomes 1.80M.

Floating-point arithmetic is not supported in bash, so I used awk instead. The problem is that I can't keep trailing zeros. Current solution:

if [ $size -ge 1048576 ] then size=$(awk 'BEGIN {printf "%.3g",'$size'/1048576}')M elif [ $size -ge 1024 ] then size=$(awk 'BEGIN {printf "%.3g",'$size'/1024}')K fi 

(Files are not so large, so I do not need to consider large units.)

Change: There is another problem with this. See Adrian Frewwirth Commentary below.

+58
linux bash
Apr 6 '13 at 18:10
source share
8 answers

GNU Coreutils contains a seemingly rather unknown little numfmt tool for numfmt conversion that does what you need:

 $ numfmt --to=iec-i --suffix=B --format="%.3f" 4953205820 4.614GiB 

I think this fits your needs well, and not as great or hacky as the other answers.

If you want a more powerful solution, look at my other answer.

+55
Dec 12 '13 at 1:27
source share

Is there a reason you are not using

 ls -lh 

team? If you are on a Linux system released in the last few years, you have this feature.

+129
Apr 6 '13 at
source share
 ls -lah /path/to/your/file | awk -F " " {'print $5'} 
+9
Feb 22 '15 at 21:00
source share

I know this is a little late. But maybe someone finds this useful.

The answer is simply to use %.2f instead of %.3g in the script. ( src )




Test:

 #!/bin/bash size=1883954 if [ $size -ge 1048576 ] then size=$(awk 'BEGIN {printf "%.2f",'$size'/1048576}')M elif [ $size -ge 1024 ] then size=$(awk 'BEGIN {printf "%.2f",'$size'/1024}')K fi echo $size 

Exit:

 1.80M 
+3
Nov 11 '15 at 15:24
source share

If you do not mind using bc , then the following operations will perform floating point operations. scale may vary according to your needs, depending on the many numbers you want to print.

 size=1883954 if [ $size -ge 1048576 ] then size=$(echo "scale=2;$size/1048576"| bc)M elif [ $size -ge 1024 ] then size=$(echo "scale=2;$size/1024" | bc)K fi echo $size 
+2
Apr 6 '13 at 18:21
source share

Instead of using ls and awk to get the file size, use stat -c %s filename.ext . It displays only the number, and nothing more (at least on version 8.21). I cannot use numfmt because it is an older version that does not use printf syntax with decimal precision. Instead, I use the script below. I use the last line to check for the source of the script. If it is not, I can call it directly on the command line.

 #!/bin/bash function getFriendlyFileSize() { OUT='/dev/null' [ "$#" == 0 ] && echo 'No number given' && return 1 [ ! $(echo $1 | egrep -i '\-?[0-9]+') ] && echo 'Garbage data' && return 1 if [ "$1" == '' -o "$1" -lt 0 ] 2>$OUT then echo '0 B' return 1 else FSIZE=$1 fi [ "$2" == '' ] && DECPTS=1 || DECPTS=$2 KB=1024 MB=1048576 GB=1073741824 TB=1099511627776 PB=1125899906842624 EB=1152921504606846976 LM=9223372036854775807 # bash comparison limit = 2^63-1 (signed int?) [ "$FSIZE" -le 0 ] 2>$OUT && echo "0 B" && return [ "$FSIZE" -lt $KB ] 2>$OUT && echo "$FSIZE B" && return [ "$FSIZE" -lt $MB ] 2>$OUT && echo "$(echo "scale=$DECPTS;$FSIZE/$KB"|bc) KB" && return [ "$FSIZE" -lt $GB ] 2>$OUT && echo "$(echo "scale=$DECPTS;$FSIZE/$MB"|bc) MB" && return [ "$FSIZE" -lt $TB ] 2>$OUT && echo "$(echo "scale=$DECPTS;$FSIZE/$GB"|bc) GB" && return [ "$FSIZE" -lt $PB ] 2>$OUT && echo "$(echo "scale=$DECPTS;$FSIZE/$TB"|bc) TB" && return [ "$FSIZE" -lt $EB ] 2>$OUT && echo "$(echo "scale=$DECPTS;$FSIZE/$PB"|bc) PB" && return [ "$FSIZE" -le $LM ] 2>$OUT && echo "$(echo "scale=$DECPTS;$FSIZE/$EB"|bc) EB" && return [ "$?" -ne '0' ] 2>$OUT && echo "Bad input" && return 1 } [[ $_ == $0 ]] && getFriendlyFileSize $1 $2 
+1
Feb 20 '17 at 4:00
source share

If you have Qalculate installed ! (which, by the way, is surprising) Theres an easy trick:

 human_readable="$( qalc -t set "precision $precision" "${in_bytes}B" )" 

Example:

 $ qalc -t -set "precision 3" 5264334820B 5.26 GB 

This is a very powerful tool for working with shells, because it even simplifies formulas, solves for the unknown, and much more.

 $ qalc -t "e^(i*x)=-1" x = 3.1415927 

If you want a simpler and less difficult solution, look at my other answer.

0
Dec 12 '13 at 12:28
source share

Preferred Solution

man stat

* Example of using the " readable format " in stat: *

stat %A custom_file.txt

Hurrah

0
Jul 19 '19 at 1:17
source share



All Articles