Skip to content

Instantly share code, notes, and snippets.

@susomena
Created February 3, 2016 10:51
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 susomena/dfb27faf46bd95f73367 to your computer and use it in GitHub Desktop.
Save susomena/dfb27faf46bd95f73367 to your computer and use it in GitHub Desktop.

Compressed File Size Sum Script

By Jesús Mena.

This script sums the size of all compressed files in a directory. It accepts three mutually exclusive and optional arguments, -K, -M and -G. This arguments are used to get the result in kilobytes, megabytes and gigabytes respectively. If no arguments are given the result is shown in bytes.

#!/bin/bash
function usage () {
cat <<EOF
Usage: $0 [-K|-M|-G]
where:
-K returns result in KB
-M returns result in MB
-G returns result in GB
Sums the size of all compressed files in the current directory.
EOF
exit 0
}
if [ "$1" = "--help" ]; then
usage
fi
factor=1
unity=''
while getopts ":KMG" opt; do
case $opt in
K) [[ -n "$unity" ]] && usage || factor=1024; unity='KB'; ;;
M) [[ -n "$unity" ]] && usage || factor=1048576; unity='MB'; ;;
G) [[ -n "$unity" ]] && usage || factor=1073741824; unity='GB' ;;
\?) echo "Invalid option: -$OPTARG"; usage ;;
esac
done
for i in *.zip; do unzip -l $i | tail -n 1; done | awk -v awk_factor=$factor -v awk_unity=$unity '{ total += $1 }; END { printf("%.2f%s\n",total/awk_factor,awk_unity) }'
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment