-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathdir-elems
executable file
·87 lines (76 loc) · 1.99 KB
/
dir-elems
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
#!/usr/bin/env bash
#=======================================================================
# dir-elems
# File ID: 6a061bba-0014-11e5-8931-0d8802d731e4
#
# Create sorted list with number of directory elements in a directory
# tree. Useful to find directories with lots of files that need to be
# split up.
#
# Author: Øyvind A. Holm <[email protected]>
# License: GNU General Public License version 2 or later.
#=======================================================================
progname=dir-elems
VERSION=0.3.0
ARGS="$(getopt -o "\
h\
q\
s\
v\
" -l "\
help,\
quiet,\
subtree,\
verbose,\
version,\
" -n "$progname" -- "$@")"
test "$?" = "0" || exit 1
eval set -- "$ARGS"
opt_help=0
opt_quiet=0
opt_subtree=0
opt_verbose=0
while :; do
case "$1" in
-h|--help) opt_help=1; shift ;;
-q|--quiet) opt_quiet=$(($opt_quiet + 1)); shift ;;
-s|--subtree) opt_subtree=1; shift ;;
-v|--verbose) opt_verbose=$(($opt_verbose + 1)); shift ;;
--version) echo $progname $VERSION; exit 0 ;;
--) shift; break ;;
*) echo $progname: Internal error >&2; exit 1 ;;
esac
done
opt_verbose=$(($opt_verbose - $opt_quiet))
if test "$opt_help" = "1"; then
test $opt_verbose -gt 0 && { echo; echo $progname $VERSION; }
cat <<END
Create sorted list with number of directory elements in a directory
tree. Useful to find directories with lots of files that need to be
split up.
Usage: $progname [options]
Options:
-h, --help
Show this help.
-q, --quiet
Be more quiet. Can be repeated to increase silence.
-s, --subtree
Count files in whole subtrees instead of single directories.
-v, --verbose
Increase level of verbosity. Can be repeated.
--version
Print version information.
END
exit 0
fi
find "$@" -type d | while read f; do
(
cd "$f" && {
if test "$opt_subtree" = "1"; then
echo $(find | wc -l) $f
else
echo $(ls -a | wc -l) $f
fi
}
)
done | sort -n