forked from JiriFurda/IOS-proj1
-
Notifications
You must be signed in to change notification settings - Fork 0
/
dirgraph
219 lines (183 loc) · 3.45 KB
/
dirgraph
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
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
#!/bin/sh
# [IOS] Project 1 - Directory Contents Report
# Author: Jiri Furda (xfurda00)
# --- Shell settings ---
LC_ALL=en_US.UTF-8
POSIXLY_CORRECT=yes
# --- Variables ---
HELP=0
N=0
# --- Processing flags ---
while getopts :i:n o
do case "$o" in
i) FILE_ERE="$OPTARG";;
n) N=1;;
*) HELP=1;;
esac
done
# --- Processing target directory ---
ARGC=$#
if [ "$ARGC" -eq 0 ]; then
DIR=.
else
if [ "$OPTIND" -eq "$ARGC" ]; then
shift $(($OPTIND - 1))
DIR="$*"
else
if [ $(($OPTIND - 1)) -eq "$ARGC" ]; then
DIR=.
else
HELP=1
fi
fi
fi
# --- Returning help ---
if [ "$HELP" -eq 1 ]; then
echo "This script creates report of directory" >&2
echo "Syntax: dirgraph [-i FILE_ERE] [-n] [DIR]" >&2
exit 1 # jedna nebo nula?
fi
# --- Check for errors ---
if [ ! -d "$DIR" ]; then
echo "Error: Directory doesn't exist" >&2
exit 1
fi
# --- Normalization (-n FLAG) ---
if [ "$N" -eq 1 ]; then
if [ -t 1 ]; then
N=$(tput cols) # Proc se to musi takhle?
else
N=80
fi
fi
# --- Printing head of result ---
echo "Root directory: $DIR"
printf "Directories: %d\n" $(find $DIR -type d | awk -v ignore="$FILE_ERE" '
{
# --- Ignore flag (-i) ---
if(ignore != "")
{
split($0,path,"/")
for(i in path)
{
if(match(path[i],ignore) != 0)
next;
}
}
print $0;
}
' | wc -l)
# --- File size histogram ---
find "$DIR" -type f -exec wc -c {} + | awk -v ignore="$FILE_ERE" -v maxcol="$N" '
BEGIN{
totalfiles = 0;
}
{
if($2 != "total")
{
filesize = $1;
if(ignore != "")
{
$1 = "";
split($0,path,"/") # prvni bude asi s mezerou
for(i in path)
{
if(match(path[i],ignore) != 0)
next;
}
}
if(filesize < 100)
size[0]++;
else if(filesize < 1024)
size[1]++;
else if(filesize < 10240)
size[2]++;
else if(filesize < 102400)
size[3]++;
else if(filesize < 1048576)
size[4]++;
else if(filesize < 10485760)
size[5]++;
else if(filesize < 104857600)
size[6]++;
else if(filesize < 1073741824)
size[7]++;
else
size[8]++;
totalfiles++;
}
}
END{
print "All files: "totalfiles;
prefix[0] = "<100 B";
prefix[1] = "<1 KiB";
prefix[2] = "<10 KiB";
prefix[3] = "<100 KiB";
prefix[4] = "<1 MiB";
prefix[5] = "<10 MiB";
prefix[6] = "<100 MiB";
prefix[7] = "<1 GiB";
prefix[8] = ">=1 GiB";
if(maxcol != 0)
{
maxhashcol = maxcol-13; # Asi osetrit
for(i = 0; i < 9; i++)
{
if(size[i] > maxsize)
maxsize = size[i];
}
}
print "File size histogram:"
for(i = 0; i < 9; i++)
{
if(maxcol == 0 || maxsize <= maxhashcol)
max = size[i];
else
max = size[i] * maxhashcol / maxsize;
printf " %-8s: ",prefix[i];
for (j = 0; j < max; j++)
{
printf "#";
}
printf "\n";
}
}'
# --- File type histogram ---
find "$DIR" -type f -exec file -F "//" {} \; | awk -F"// " -v ignore="$FILE_ERE" '
{
# --- Ignore flag (-i) ---
if(ignore != "")
{
split($1,path,"/")
for(i in path)
{
if(match(path[i],ignore) != 0)
next;
}
}
# --- Shortening file info ---
if(length($2) > 40)
$2 = substr($2, 0, 40)"...";
print $2;
}' | sort | uniq -c | sort -n -r | head -n 10 | awk -v maxcol="$N" '
BEGIN{
if(maxcol != 0)
maxhashcol = maxcol-48; # Asi osetrit
print "File type histogram:"
}
{
if(NR==1)
maxtype = $1;
if(maxcol == 0 || maxtype <= maxhashcol)
max = $1;
else
max = $1 * maxhashcol / maxtype;
$1 = ""
printf " %-44s: ",$0;
for(j = 0; j < max; j++)
{
printf "#";
}
printf "\n";
}'
exit 0