-
-
Save jth0/ffb1941515d097266a42 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
__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)) | |
[ ! -x "/usr/bin/bc" ] && echo -e "You need bc installed for the sizing your ups." && return # because you do and I didn't | |
# local cmd="find . -type f ${depth}$(__sizeup_build_query $@)" # returned error "find: warning: you have specified the -maxdepth option after a non-option argument -type, but options are not positional (-maxdepth affects tests specified before it as well as those specified after it). Please specify options before other arguments." | |
local cmd="find . ${depth} -type f $(__sizeup_build_query $@)" | |
local counter=0 | |
while read -r file; do | |
counter=$(( $counter+1 )) | |
if [[ $OS == Windows_NT ]]; then size=$(stat -c '%s' "$file"); # for cygwin | |
else size=$(stat -f '%z' "$file"); fi # for not cygwin | |
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
Revised to add support in Cygwin.