-
Notifications
You must be signed in to change notification settings - Fork 16
/
du-top-10.sh
executable file
·84 lines (67 loc) · 1.43 KB
/
du-top-10.sh
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
#!/bin/bash
# find large files
TOP_10_MSG="top 10 by size"
INPUT_PATH="."
if [ -z "${NO_SPINNER}" ]; then
NO_SPINNER=0
fi
if [ -n "$1" ]; then
if [ ! -d "$*" ]; then
echo >&2 "usage: $0 [directory]"
exit 1
fi
INPUT_PATH=$(readlink -m "$@")
echo "$TOP_10_MSG: $INPUT_PATH"
else
INPUT_PATH=$(readlink -m "$INPUT_PATH")
echo "$TOP_10_MSG"
fi
trap cleanup EXIT INT TERM
cleanup() {
if [ -n "$SCRIPT_PID" ]; then
kill "$SCRIPT_PID"
fi
exit $?
}
run_spinner()
{
local msg="$1"
local delay=0.5
local SP='\|/-';
while [ "$SPIN" == "true" ]
do
printf "\b\r[%s] %s" "${SP:i++%${#SP}:1}" "${msg}"
sleep $delay;
done
}
start_spinner()
{
SPIN="true"
run_spinner "$1" &
SCRIPT_PID=$!
}
stop_spinner()
{
SPIN="false"
# clear current line of any text
echo -ne "\r%\033[0K\r"
}
if [ "$NO_SPINNER" -eq 0 ]; then
start_spinner "Calculating directory sizes ..."
fi
INPUT_PATH_ESCAPED=""
if [ "$INPUT_PATH" != "/" ]; then
INPUT_PATH=${INPUT_PATH}"/"
INPUT_PATH_ESCAPED=$(readlink -m "${INPUT_PATH}" | sed -e 's/\//\\\//g' -e 's/\./\\\./g')
fi
pushd "${INPUT_PATH}" > /dev/null 2>&1
RAW=$(du -shx ./.[^.]* 2>/dev/null ; du -shx ./[^.]* 2>/dev/null)
OUTPUT=$(echo "${RAW}" | sed "s/\.\//${INPUT_PATH_ESCAPED}\//g" \
| LC_ALL=C sort -k2 | sort -rh \
| head -10)
popd > /dev/null 2>&1
if [ "$NO_SPINNER" -eq 0 ]; then
stop_spinner
fi
echo "$OUTPUT"
exit 0