Files
q-tools/files/fastdu
2017-04-26 10:00:18 +03:00

60 lines
1.5 KiB
Bash
Executable File

#!/bin/bash
function helpexit() {
echo "Display file/folder sizes like 'du', but faster and actual byte sizes"
echo "Returns only the size as number"
echo " -h Human readable units"
echo " -s Summary mode, otherwise each argument folder calculated separate"
echo " --help This help"
exit
}
function filesize {
# Return a human readable size from integer of bytes
# Usage: filesize 10000
[ "$1" = "0" ] && {
echo "0 B"
return 0
}
awk 'BEGIN{ x = '$1'
split("B KB MB GB TB PB EB ZB",type)
for(i=7;y < 1;i--)
y = x / (2^(10*i))
str=int(y*10)/10 " " type[i+2]
if (x==0) { str = "0 B" }
print str
}' || return $?
}
function displaysize() {
SIZE=$( find "$@" -type f -printf %s"\n" | awk '{ sum += $1 } END { print int(sum)+0 }' )
[[ "$HUMAN" -eq 1 ]] && {
SIZE=$( filesize "$SIZE" )
}
echo "$SIZE"
}
# Get options
what=()
for (( i=1; i<=$#; i++ )); do
[[ "${!i}" = "--help" ]] && helpexit
[[ "${!i}" = "-h" ]] && { HUMAN=1; continue; }
[[ "${!i}" = "-s" ]] && { SUMMARY=1; continue; }
what+=( "${!i}" )
done
[[ -z "${what[@]}" ]] && what="."
# If only one entry, dont print the name
[[ "${#what[@]}" -eq 1 ]] && SUMMARY=1
if [[ "$SUMMARY" -eq 1 ]]; then
# Display one line
displaysize "${what[@]}"
else
# One size for each argument
for dir in "${what[@]}"; do
printf "%s\t%s\n" "$( displaysize "$dir" )" "$dir"
done
fi