-
Notifications
You must be signed in to change notification settings - Fork 0
/
dirgraph
executable file
·387 lines (332 loc) · 9.3 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
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
#!/bin/bash
###################################################################
#Script Name : dirgraph
#
#Description : Counts the number of subdirectories and files in
# specified directory. The output consist of drawn
# histogram where all files are sorted according their
# size. The does not follw symbolic links and hidden
# files or directories. The histogram can be
# normalized when parameter -n is provided. Certain
# filenames or directory can be skipped when parameter
# -i with valid regular expression is provided. When
# no specific directory is given, the script begin
# searching in current ($PWD) directory.
#
#Args :[-i FILE_ERE] [-n] [DIR]
# FILE_ERE -valid regular expression
# -n -toggles histogram normalization to fit graph into
# a width the output
# DIR -specifies the directory to start search in
#
#Author : Vladislav Valek
#Email : [email protected]
###################################################################
#nasteveni konzole
set -u -o pipefail
# -o pipefail navratova hodnoty pipeline je urcena poslednim prikazem
# -u povazuje nenastavene vychozi hodnoty promennych za chybu
# -x umoznuje sledovani prubehu vykonavani prikazu a jejich tisk na konzoli
export POSIXLY_CORRECT=YES
count_directories () { #funkce pro pocitani slozek
num=`find "$1" -type d 2>/dev/null | egrep -v "$2" | wc -l`
exit_code=$?
printf "Directories: %d\n" $num
return $exit_code
}
count_files () { #funkce pro pocitani souboru
num=`find "$1" -type f 2>/dev/null | egrep -v "$2" | wc -l`
exit_code=$?
printf "All files: %d\n" $num
return $exit_code
}
make_fhist () {
file_list=`find "$1" -type f -print 2>/dev/null | egrep -v "$3"`
ret_val1=$? #navratova hodnota posledniho prikazu urcuje navratovy kod dane funkce
typeset -A cn=([lt100]=0 [lt1Ki]=0 [lt10Ki]=0 [lt100Ki]=0 [lt1Mi]=0 [lt10Mi]=0 [lt100Mi]=0 [lt1Gi]=0 [gt1Gi]=0)
ret_val2=0
byte_num=0
while IFS=$'\n' read -r row
do
byte_num=`wc -c "$row" 2>/dev/null | awk '{print $1}'`
ret_val2=$?
if [ $ret_val2 -ne 0 ]
then
continue
fi
if (( $byte_num < 100 ))
then
cn[lt100]=`expr ${cn[lt100]} + 1`
elif (( $byte_num >= 100 && $byte_num < 1024 ))
then
cn[lt1Ki]=`expr ${cn[lt1Ki]} + 1`
elif (( $byte_num >= 1024 && $byte_num < 10240 ))
then
cn[lt10Ki]=`expr ${cn[lt10Ki]} + 1`
elif (( $byte_num >= 10240 && $byte_num < 102400 ))
then
cn[lt100Ki]=`expr ${cn[lt100Ki]} + 1`
elif (( $byte_num >= 102400 && $byte_num < 1048576 ))
then
cn[lt1Mi]=`expr ${cn[lt1Mi]} + 1`
elif (( $byte_num >= 1048576 && $byte_num < 10485760 ))
then
cn[lt10Mi]=`expr ${cn[lt10Mi]} + 1`
elif (( $byte_num >= 10485760 && $byte_num < 104857600 ))
then
cn[lt100Mi]=`expr ${cn[lt100Mi]} + 1`
elif (( $byte_num >= 104857600 && $byte_num < (1024*1048576) ))
then
cn[lt1Gi]=`expr ${cn[lt1Gi]} + 1`
else
cn[gt1Gi]=`expr ${cn[gt1Gi]} + 1`
fi
done <<< "$file_list"
cols=1 #pocet sloupcu
catname_length=11 #delka nazvu kategorie
norm_on="no" #rozhodnuti o normalizaci
if [ "$2" = "-n" ] #podminka, zda chci normalizovat graf
then
if [ -t 1 ] #pokud skript bezi v terminalu
then
cols=`expr $(tput cols) - 1 - $catname_length`
else #pokud bezi skript nekde jinde
cols=79
fi
max=0
for idx in ${!cn[@]} #naleznu maximum
do
if [ ${cn[$idx]} -gt $max ]; then max=${cn[$idx]}; fi
done
cf=0
if [ $max -gt $cols ]; then #pokud je max vetsi jak delka radku, zacnu normalizovat
norm_on="yes"
cf=$(echo "scale=5; $cols / $max" | bc)
fi
for idx in "${!cn[@]}" #normalizuji...
do
if [ "$norm_on" = "yes" ]
then
cn[$idx]=$(echo "scale=5; ${cn[$idx]} * $cf" | bc)
b=${cn[$idx]} #value before rounding
cn[$idx]=$(LC_NUMERIC="en_US.UTF-8" printf "%.0f" "${cn[$idx]}")
a=${cn[$idx]} #value after rounding
res1=$(echo "if($b!=0) 1" | bc -l)
res2=$(echo "if($a==0) 1" | bc -l)
if [ "$res1" = "1" -a "$res2" = "1" ] #zaokrouhlovani na jednicku
then
cn[$idx]=1
fi
else
break
fi
done
fi
echo "File size histogram:"
printf " %s%3s " "<100 B" ":"
idx=0
while [ $idx -lt ${cn[lt100]} ]; do echo -n "#"; idx=`expr $idx + 1`; done
echo
printf " %s%3s " "<1 KiB" ":"
idx=0
while [ $idx -lt ${cn[lt1Ki]} ]; do echo -n "#"; idx=`expr $idx + 1`; done
echo
printf " %s%2s " "<10 KiB" ":"
idx=0
while [ $idx -lt ${cn[lt10Ki]} ]; do echo -n "#"; idx=`expr $idx + 1`; done
echo
printf " %s%s " "<100 KiB" ":"
idx=0
while [ $idx -lt ${cn[lt100Ki]} ]; do echo -n "#"; idx=`expr $idx + 1`; done
echo
printf " %s%3s " "<1 MiB" ":"
idx=0
while [ $idx -lt ${cn[lt1Mi]} ]; do echo -n "#"; idx=`expr $idx + 1`; done
echo
printf " %s%2s " "<10 MiB" ":"
idx=0
while [ $idx -lt ${cn[lt10Mi]} ]; do echo -n "#"; idx=`expr $idx + 1`; done
echo
printf " %s%s " "<100 MiB" ":"
idx=0
while [ $idx -lt ${cn[lt100Mi]} ]; do echo -n "#"; idx=`expr $idx + 1`; done
echo
printf " %s%3s " "<1 GiB" ":"
idx=0
while [ $idx -lt ${cn[lt1Gi]} ]; do echo -n "#"; idx=`expr $idx + 1`; done
echo
printf " %s%2s " ">=1 GiB" ":"
idx=0
while [ $idx -lt ${cn[gt1Gi]} ]; do echo -n "#"; idx=`expr $idx + 1`; done
echo
if [ $ret_val1 -ne 0 -o $ret_val2 -ne 0 ]
then
return 1 #navratova hodnota cele funkce
else
return 0
fi
}
if [ $# -eq 0 ] #pripad, kdy nebyl nalezen zadny parametr, skript bere aktualni adresar
then
echo "Root directory: $PWD"
count_directories "$PWD" '!.'
count_files "$PWD" '!.'
make_fhist "$PWD" x '!.'
exit $?
elif [ $# -eq 1 ] #byl zadan jeden pozicni parametr
then
if [ "$1" = "-n" ] #pozicni parametr je prepinac normalizace grafu
then
echo "Root directory: $PWD"
count_directories "$PWD" '!.'
count_files "$PWD" '!.'
make_fhist "$PWD" $1 '!.'
exit $?
elif [ -d "$1" ] #pozicni parametr je nazev adresare
then
echo "Root directory: $1"
count_directories "$1" '!.'
count_files "$1" '!.'
make_fhist "$1" x '!.'
exit $?
elif [[ "$1" =~ [-*] ]]
then
echo "Invalid option!" >/dev/stderr
exit 64
else
echo "$1:Not a name of a directory!" >/dev/stderr
exit 66
fi
elif [ $# -eq 2 ] #zadany dva pozicni parametry
then
if [ "$1" = "-i" ]
then
if [[ -n "$2" ]] && [[ ! "$2" =~ [-*] ]] #test, zda je retezec za prepinacem nenulovy
then
if [ "$2" = "/" ] #nesmi pokryvat nazev korenoveho adresare
then
echo "Ignored-files list should not include a name of the root directory!" >/dev/stderr
exit 64
else
echo "Root directory: $PWD"
count_directories "$PWD" "$2"
count_files "$PWD" "$2"
make_fhist "$PWD" x "$2"
exit $?
fi
else
echo "Valid regular expression must follow!" >/dev/stderr
exit 65
fi
elif [ "$1" = "-n" ]
then
if [ -d "$2" ] #testovani, zda druhy pozicni parametr je nazev adresare
then
echo "Root directory: $2"
count_directories "$2" '!.'
count_files "$2" '!.'
make_fhist "$2" $1 '!.'
exit $?
else
echo "$2:Not a name of a directory!" >/dev/stderr
exit 66 #66- neexistujici jmeno adresare
fi
elif [[ "$1" =~ [-*] ]]
then
echo "Invalid option!" >/dev/stderr
exit 64
else
echo "Unrecognized action!" >/dev/stderr
exit 67
fi
elif [ $# -eq 3 ] #zadany tri pozicni parametry
then
if [ "$1" = "-i" ]
then
if [[ -n "$2" ]] && [[ ! "$2" =~ [-*] ]] #test, zda je retezec za prepinacem nenulovy
then
if [ "$2" = "/" ]
then
echo "Ignored-files list should not include a name of the root directory!" >/dev/stderr
exit 64
else
if [ "$3" = "-n" ]
then
echo "Root directory: $PWD"
count_directories "$PWD" "$2"
make_fhist "$PWD" $3 "$2"
exit $?
elif [ -d "$3" ]
then
echo "Root directory: $3"
count_directories "$3" "$2"
count_files "$3" "$2"
make_fhist "$3" x "$2"
exit $?
elif [[ "$3" =~ [-*] ]]
then
echo "$3:Not a valid parameter!" >/dev/stderr
exit 64 # 64 - spatny parametr
else
echo "Invalid parameter!"
exit 64
fi
fi
else
echo "Valid regular expression must follow!" >/dev/stderr
exit 65 # 65- nepresny nebo nulovy regularni vyraz
fi
elif [[ "$1" =~ [-*] ]]
then
echo "Invalid option!" >/dev/stderr
exit 64
else
echo "Wrong order of parameters, correct form is: dirgraph [-i FILE_ERE] [-n] [DIR]" >/dev/stderr
exit 67
fi
elif [ $# -eq 4 ]
then
if [ "$1" = "-i" ]
then
if [[ -n "$2" ]] && [[ ! "$2" =~ [-*] ]] #test, zda je retezec za prepinacem nenulovy
then
if [ "$2" = "/" ] #nesmi pokryvat nazev korenoveho adresare
then
echo "Ignored-files list should not include a name of the root directory!" >/dev/stderr
exit 64
else
if [ "$3" = "-n" ]
then
if [ -d "$4" ]
then
echo "Root directory: $4"
count_directories "$4" "$2"
count_files "$4" "$2"
make_fhist "$4" $3 "$2"
exit $?
else
echo "$4: Not a name of a directory!" >/dev/stderr
exit 66
fi
else
echo "$3: Wrong parameter!" >/dev/stderr
exit 64
fi
fi
else
echo "Valid regular expression must follow!" >/dev/stderr
exit 65
fi
elif [[ "$1" =~ [-*] ]]
then
echo "Invalid option!" >/dev/stderr
exit 64
else
echo "Wrong order of parameters, correct form is: dirgraph [-i FILE_ERE] [-n] [DIR]" >/dev/stderr
exit 67
fi
else
echo "Too much parameters!" >/dev/stderr
exit 67
fi
exit 1 #dostat se sem je chyba!