-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdusize
executable file
·92 lines (79 loc) · 2.08 KB
/
dusize
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
#!/usr/bin/env bash
# dusize - Ordered listing of disk usage
# (c) 2006-2018 Alex Malinovich (alex@malinovich.name)
# Released under the GPL v2 or later
# See www.fsf.org for a full copy of the GPL.
DUOPTS="-xk"
SORTOPTS="-g"
FORCE_AWK=false
DEBUG=false
x=1 # Avoids an error if we get no options at all.
while getopts "artdh" opt; do
case "$opt" in
a) FORCE_AWK=true;;
r) SORTOPTS="${SORTOPTS}r";;
t) MAXDEPTH=0;;
d) DEBUG=true;;
h|\?) printf 'Usage: %s [-a] [-r] [-t] [-d] PATHS...
-a Force awk to be used even if sort supports -h
-r Reverse sort
-t Display total usage only
-d Debug mode (print command to be run)\n' "$(basename "$0")"; exit 0;;
esac
x=$OPTIND
done
shift $(($x-1))
[ -n "$SORTCMD" -a -x "$SORTCMD" ] && hash -p "$SORTCMD" sort 2>/dev/null
[ -n "$DUCMD" -a -x "$DUCMD" ] && hash -p "$DUCMD" du 2>/dev/null
[ -n "$AWKCMD" -a -x "$AWKCMD" ] && hash -p "$AWKCMD" awk 2>/dev/null && FORCE_AWK=true
if ! sort -h < /dev/null 2>/dev/null && hash gsort 2>/dev/null; then
hash -p "$(hash -t gsort)" sort
fi
if ! $FORCE_AWK && sort -h < /dev/null 2>/dev/null; then
SORTOPTS="${SORTOPTS/#-g/-h}"
DUOPTS="${DUOPTS}h"
else
FORCE_AWK=true
fi
# `for` without `in` defaults to iterating through $@
for i; do
if [ ! -z "$i" ] && [ -d "$i" ]; then
SEARCHDIR+=("${i%%/}")
fi
shift
done
if [ -z "${SEARCHDIR[*]}" ]; then
SEARCHDIR=(".")
fi
if $DEBUG; then
hash -t du &>/dev/null || hash du 2>/dev/null
hash -t sort &>/dev/null || hash sort 2>/dev/null
printf '%s %s -d %i %s | %s %s' "$(hash -t du)" "$DUOPTS" ${MAXDEPTH:-1} "${SEARCHDIR[@]}" "$(hash -t sort)" "$SORTOPTS"
if $FORCE_AWK; then
hash -t awk &>/dev/null || hash awk 2>/dev/null
printf ' | %s\n' "$(hash -t awk)"
else
printf '\n'
fi
fi
du "$DUOPTS" -d ${MAXDEPTH:-1} "${SEARCHDIR[@]}" | sort $SORTOPTS | if [[ "$SORTOPTS" == *"-h"* ]]; then cat -; else
awk 'BEGIN{
suff[0]="KiB"
suff[1]="MiB"
suff[2]="GiB"
suff[3]="TiB"
suff[4]="PiB"
}
{
size=$1
count=0
while (size > 1023) {
count++
size=size/1024
}
printf("%6.1f %s\t", size, suff[count])
for (i=2; i <= NF; i++)
printf("%s ", $i)
print ""
}'
fi