Skip to content

Instantly share code, notes, and snippets.

@ttscoff
Created January 5, 2015 16:48
Show Gist options
  • Save ttscoff/037f21db04f5072a5885 to your computer and use it in GitHub Desktop.
Save ttscoff/037f21db04f5072a5885 to your computer and use it in GitHub Desktop.
A shell function for viewing file sizes and totals within a nested directory structure
__sizeup_build_query () {
local bool="and"
local query=""
for t in $@; do
query="$query -$bool -iname \"*.$t\""
bool="or"
done
echo -n "$query"
}
__sizeup_humanize () {
local size=$1
if [ $size -ge 1073741824 ]; then
printf '%6.2f%s' $(echo "scale=2;$size/1073741824"| bc) G
elif [ $size -ge 1048576 ]; then
printf '%6.2f%s' $(echo "scale=2;$size/1048576"| bc) M
elif [ $size -ge 1024 ]; then
printf '%6.2f%s' $(echo "scale=2;$size/1024"| bc) K
else
printf '%6.2f%s' ${size} b
fi
}
sizeup () {
local helpstring="Show file sizes for all files with totals\n-r\treverse sort\n-[0-3]\tlimit depth (default 4 levels, 0=unlimited)\nAdditional arguments limit by file extension\n\nUsage: sizeup [-r[0123]] ext [,ext]"
local totalb=0
local size output reverse OPT
local depth="-maxdepth 4"
OPTIND=1
while getopts "hr0123" opt; do
case $opt in
r) reverse="-r " ;;
0) depth="" ;;
1) depth="-maxdepth 1" ;;
2) depth="-maxdepth 2" ;;
3) depth="-maxdepth 3" ;;
h) echo -e $helpstring; return;;
\?) echo "Invalid option: -$OPTARG" >&2; return 1;;
esac
done
shift $((OPTIND-1))
local cmd="find . -type f ${depth}$(__sizeup_build_query $@)"
local counter=0
while read -r file; do
counter=$(( $counter+1 ))
size=$(stat -f '%z' "$file")
totalb=$(( $totalb+$size ))
>&2 echo -ne $'\E[K\e[1;32m'"${counter}:"$'\e[1;31m'" $file "$'\e[0m'"("$'\e[1;31m'$size$'\e[0m'")"$'\r'
# >&2 echo -n "$(__sizeup_humanize $totalb): $file ($size)"
# >&2 echo -n $'\r'
output="${output}${file#*/}*$size*$(__sizeup_humanize $size)\n"
done < <(eval $cmd)
>&2 echo -ne $'\r\E[K\e[0m'
echo -e "$output"| sort -t '*' ${reverse}-nk 2 | cut -d '*' -f 1,3 | column -s '*' -t
echo $'\e[1;33;40m'"Total: "$'\e[1;32;40m'"$(__sizeup_humanize $totalb)"$'\e[1;33;40m'" in $counter files"$'\e[0m'
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment