gzsize
shows gzipped file sizes of files in your current working directory.
# For terminal
gzsize
# Markdown table
gzsize -m
# Raw byte size
gzsize -r
#!/usr/bin/env bash | |
POSITIONAL=() | |
while [[ $# -gt 0 ]]; do | |
key="$1" | |
case $key in | |
-r|--raw) | |
RAW=YES | |
shift | |
;; | |
-m|--markdown) | |
MARKDOWN=YES | |
shift | |
;; | |
*) | |
POSITIONAL+=("$1") | |
shift | |
;; | |
esac | |
done | |
# Restore positional parameters | |
set -- "${POSITIONAL[@]}" | |
# http://www.kossboss.com/?p=1844 | |
human () { | |
echo $1 | awk '{xin=$1;if(xin==0){print "0B";}else{x=(xin<0?-xin:xin);s=(xin<0?-1:1);split("B KiB MiB GiB TiB PiB",type);for(i=5;y < 1;i--){y=x/(2^(10*i));}print y*s "" type[i+2];};}' | |
} | |
passthrough () { | |
echo $1 | |
} | |
list () { | |
# https://unix.stackexchange.com/questions/197792/joining-bash-arguments-into-single-string-with-spaces | |
IFS=" " | |
echo "$*" | |
} | |
table () { | |
printf "|" | |
for a in $@; do | |
printf "$a|" | |
done | |
echo | |
} | |
mdheader () { | |
table "$*" | |
printf "|" | |
for a in $@; do | |
printf -- "-|" | |
done | |
echo | |
} | |
output () { | |
header=$1 | |
row=$2 | |
$header FILE RAW GZIP | |
for file in ./* | |
do | |
if [ -f $file ]; then | |
raw=$(stat -f '%z' $file) | |
gzipped=$(gzip -c $file | wc -c) | |
$row $(basename $file) $($bytes $raw) $($bytes $gzipped) | |
fi | |
done | |
} | |
if [ $RAW ]; then | |
bytes=passthrough | |
else | |
bytes=human | |
fi | |
if [ $MARKDOWN ]; then | |
output mdheader table | |
else | |
output list list | column -t -s ' ' | |
fi |