Skip to content

Instantly share code, notes, and snippets.

@shuhei
Last active June 26, 2018 10:44
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save shuhei/e6247467a0f2148ad8439131114dd4e4 to your computer and use it in GitHub Desktop.
Save shuhei/e6247467a0f2148ad8439131114dd4e4 to your computer and use it in GitHub Desktop.
Show gzipped file size

gzsize

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
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment