#!/usr/bin/bash
# $Id: fsize, v1.2, 2019/12/13, gn Exp $
#
# Print the file size of any file
# Requires 'stat' and 'bc'
#
# Copyright (C) 2005-2019, Grozdan Nikolov
# License: GNU GPLv2+

STAT="$(which stat 2>/dev/null)"
BC="$(which bc 2>/dev/null)"
if [ ! -x "$STAT" -o ! -x "$BC" ]; then
	echo "-> Utility 'stat' or 'bc' is missing!"
	exit 1
fi

if [ -z "$1" ]; then
	echo "Usage: $(basename $0) /path/to/file"
	exit 1
fi

if [ ! -e "$1" ]; then
	echo "-> No such file or directory!"
	exit 1
fi

SIZE="$($STAT -c %s "$(readlink -e "$1")" 2>/dev/null)"
echo
echo "In Bits  (b) --------> $(($SIZE*8))"
echo "In Bytes (B) --------> $SIZE"
echo "In Kibibytes (KiB) --> $(echo "scale=3; $SIZE/1024" | $BC -l | sed 's/^\./0./')"
echo "In Mebibytes (MiB) --> $(echo "scale=3; $SIZE/1048576" | $BC -l | sed 's/^\./0./')"
echo "In Gibibytes (GiB) --> $(echo "scale=3; $SIZE/1073741824" | $BC -l | sed 's/^\./0./')"
echo "In Tebibytes (TiB) --> $(echo "scale=5; $SIZE/1099511627776" | $BC -l | sed 's/^\./0./')"
echo "In Pebibytes (PiB) --> $(echo "scale=8; $SIZE/1125899906842624" | $BC -l | sed 's/^\./0./')"
echo "In Exbibytes (EiB) --> $(echo "scale=11; $SIZE/1125899906842624/1024" | $BC -l | sed 's/^\./0./')"
echo "In Zebibytes (ZiB) --> $(echo "scale=14; $SIZE/1125899906842624/1024/1024" | $BC -l | sed 's/^\./0./')"
echo "In Yobibytes (YiB) --> $(echo "scale=17; $SIZE/1125899906842624/1024/1024/1024" | $BC -l | sed 's/^\./0./')"
echo
