fastdu also counts files
This commit is contained in:
44
files/fastdu
44
files/fastdu
@@ -8,7 +8,7 @@ function helpexit() {
|
|||||||
echo " --help This help"
|
echo " --help This help"
|
||||||
exit
|
exit
|
||||||
}
|
}
|
||||||
function filesize {
|
function filesize() {
|
||||||
# Return a human readable size from integer of bytes
|
# Return a human readable size from integer of bytes
|
||||||
# Usage: filesize 10000
|
# Usage: filesize 10000
|
||||||
|
|
||||||
@@ -18,7 +18,7 @@ function filesize {
|
|||||||
}
|
}
|
||||||
|
|
||||||
awk 'BEGIN{ x = '$1'
|
awk 'BEGIN{ x = '$1'
|
||||||
split("B KB MB GB TB PB EB ZB",type)
|
split("B kB MB GB TB PB EB ZB",type)
|
||||||
for(i=7;y < 1;i--)
|
for(i=7;y < 1;i--)
|
||||||
y = x / (2^(10*i))
|
y = x / (2^(10*i))
|
||||||
str=int(y*10)/10 " " type[i+2]
|
str=int(y*10)/10 " " type[i+2]
|
||||||
@@ -26,6 +26,41 @@ function filesize {
|
|||||||
print str
|
print str
|
||||||
}' || return $?
|
}' || return $?
|
||||||
}
|
}
|
||||||
|
function siprefix() {
|
||||||
|
# Return a SI prefixed string from integer
|
||||||
|
|
||||||
|
[ "$1" = "0" ] && {
|
||||||
|
echo "0"
|
||||||
|
return 0
|
||||||
|
}
|
||||||
|
|
||||||
|
awk 'BEGIN{ x = '$1'
|
||||||
|
split("_ k M G T P E Z",type)
|
||||||
|
for(i=7;y < 1;i--)
|
||||||
|
y = x / (10^(3*i))
|
||||||
|
str=int(y*10)/10 " " type[i+2]
|
||||||
|
if (x==0) { str = "0" }
|
||||||
|
if (i==-1) { str = x }
|
||||||
|
print str
|
||||||
|
}' || return $?
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
function processfolder() {
|
||||||
|
if [[ "$COUNT" -eq 1 ]]; then
|
||||||
|
countfiles "$@"
|
||||||
|
else
|
||||||
|
displaysize "$@"
|
||||||
|
fi
|
||||||
|
}
|
||||||
|
|
||||||
|
function countfiles() {
|
||||||
|
SIZE=$( find "$@" -type f -printf %s"\n" | awk '{ sum += 1 } END { print int(sum)+0 }' )
|
||||||
|
[[ "$HUMAN" -eq 1 ]] && {
|
||||||
|
SIZE=$( siprefix "$SIZE" )
|
||||||
|
}
|
||||||
|
echo "${SIZE}"
|
||||||
|
}
|
||||||
|
|
||||||
function displaysize() {
|
function displaysize() {
|
||||||
SIZE=$( find "$@" -type f -printf %s"\n" | awk '{ sum += $1 } END { print int(sum)+0 }' )
|
SIZE=$( find "$@" -type f -printf %s"\n" | awk '{ sum += $1 } END { print int(sum)+0 }' )
|
||||||
@@ -40,6 +75,7 @@ what=()
|
|||||||
for (( i=1; i<=$#; i++ )); do
|
for (( i=1; i<=$#; i++ )); do
|
||||||
[[ "${!i}" = "--help" ]] && helpexit
|
[[ "${!i}" = "--help" ]] && helpexit
|
||||||
[[ "${!i}" = "-h" ]] && { HUMAN=1; continue; }
|
[[ "${!i}" = "-h" ]] && { HUMAN=1; continue; }
|
||||||
|
[[ "${!i}" = "-c" ]] && { COUNT=1; continue; }
|
||||||
[[ "${!i}" = "-s" ]] && { SUMMARY=1; continue; }
|
[[ "${!i}" = "-s" ]] && { SUMMARY=1; continue; }
|
||||||
what+=( "${!i}" )
|
what+=( "${!i}" )
|
||||||
done
|
done
|
||||||
@@ -49,11 +85,11 @@ done
|
|||||||
|
|
||||||
if [[ "$SUMMARY" -eq 1 ]]; then
|
if [[ "$SUMMARY" -eq 1 ]]; then
|
||||||
# Display one line
|
# Display one line
|
||||||
displaysize "${what[@]}"
|
processfolder "${what[@]}"
|
||||||
else
|
else
|
||||||
# One size for each argument
|
# One size for each argument
|
||||||
for dir in "${what[@]}"; do
|
for dir in "${what[@]}"; do
|
||||||
printf "%s\t%s\n" "$( displaysize "$dir" )" "$dir"
|
printf "%s\t%s\n" "$( processfolder "$dir" )" "$dir"
|
||||||
done
|
done
|
||||||
fi
|
fi
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user