-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtreesize
executable file
·68 lines (56 loc) · 1.4 KB
/
treesize
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
#!/bin/bash
# see also "ncdu". ncurses based treesize
trapCtrlC() {
echo
echo "Aborting.."
echo
exit
}
# Catch CTRL-C
trap trapCtrlC SIGINT
# if no directory is supplied, use current directory
if [ ! "$1" ]; then
set '.'
fi
# get absolute path
DIR=$(readlink -f "$1")/
# beautify
[ "$DIR" == "//" ] && DIR=/
cd "$DIR" 2> /dev/null || {
echo -e "Error: Could not access '$DIR'.\n" >&2
exit 1
}
echo -n "Scanning..";
# original source: http://blog.aclarke.eu/a-simple-treesize-shell-script-for-linux/
du -sk "$DIR".[!.]* "$DIR"* 2>/dev/null | sort -n | awk '
BEGIN {
split("KB,MB,GB,TB", Units, ",");
}
{
if (skip == "") print " [Done]\n";
skip="true";
sum+=$1
u = 1;
while ($1 >= 1024) {
$1 = $1 / 1024;
u += 1
}
$1 = sprintf("%6.1f %s ", $1, Units[u]);
# print size and basename of directory
printf("%s ", $1)
$0 = substr($0, index($0,$1)); # truncate $0 to get basename; $1 may not contain whole directory names, e.g. when a space character is included.
system("basename \""$0"\"");
# print size and full path of directory
# print $0;
}
END {
u = 1;
while (sum >= 1024) {
sum = sum / 1024;
u += 1
}
# decimal 27 is the ASCII codepoint for the escape character
printf("\n%c[0;32m%6.1f %s Summary%c[0m\n\n", 27, sum, Units[u], 27);
# same as above, but no color
# printf("\n%6.1f %s Summary\n\n", sum, Units[u]);
}'