Skip to content

Instantly share code, notes, and snippets.

@noodny
Created November 8, 2017 09:00
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save noodny/e4727630272877c38d22e0d96087c269 to your computer and use it in GitHub Desktop.
Save noodny/e4727630272877c38d22e0d96087c269 to your computer and use it in GitHub Desktop.
Use CURL to get file size of a given url
#!/bin/sh
bytesToHuman() {
b=${1:-0}; d=''; s=0; S=(Bytes {K,M,G,T,E,P,Y,Z}iB)
while ((b > 1024)); do
d="$(printf ".%02d" $((b % 1024 * 100 / 1024)))"
b=$((b / 1024))
let s++
done
echo "$b$d ${S[$s]}"
}
URL=$1
if [ -z "$URL" ]; then
printf "\nUsage:\n\t file-size URL\n\n"
exit 0;
fi
NO_GZIP=$(curl "$URL" --silent --write-out '%{size_download}\n' --output /dev/null)
GZIP=$(curl "$URL" --silent -H 'Accept-Encoding: gzip,deflate' --write-out '%{size_download}\n' --output /dev/null)
echo "regular: $(bytesToHuman $NO_GZIP)"
echo "gzipped: $(bytesToHuman $GZIP)"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment