Format bytes using IEC units (KiB, MiB, GiB) based on powers of 1024.
Code
Utilitiesunits=(B KiB MiB GiB TiB)
size=$bytes
i=0
while [[ $(echo "$size >= 1024" | bc) -eq 1 ]] && [[ $i -lt 4 ]]; do
size=$(echo "scale=2; $size / 1024" | bc)
((i++))
done
printf "%.2f %s\n" "$size" "${units[$i]}"Parameters
Size in bytes.
Server
More Bash Snippets
Format Ordinal
Convert a number to its ordinal form (1st, 2nd, 3rd, etc.).
Mask Credit Card
Mask a credit card number for display, showing only the last 4 digits.
Pluralize Word
Add a plural suffix to a word based on the given count.
Array Difference
Find elements in the first array that are not present in the second array.
Array Frequencies
Count how many times each value appears in an array and return a frequency map.
Array Head
Get the first n elements of an array.