-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfind-ext.sh
executable file
·97 lines (85 loc) · 1.77 KB
/
find-ext.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
85
86
87
88
89
90
91
92
93
94
95
96
97
#!/usr/bin/env bash
#
# Copyright (c) Sebastian Kucharczyk <kuchen@kekse.biz>
# https://kekse.biz/ https://github.com/kekse1/scripts/
# v0.1.5
#
# Syntax: `$0 [ --depth / -d <depth> ] [ --raw / -r ]`
#
# Creates a list of all found extensions. Searching within and below the current working directory (with optional max --depth/-d).
# The '--raw / -r' parameter will prevent any other output (but the list of different extensions itself).
#
#
real="$(realpath "$0")"
dir="$(dirname "$real")"
base="$(basename "$real")"
#
result=""
count=0
raw=0
max=""
short=hrd:
long=help,raw,depth:
opts="$(getopt -o "$short" -l "$long" -n "$base" -- "$@")"
if [[ $? -eq 0 ]]; then
eval set -- "$opts"
else
exit 1
fi
while true; do
case "$1" in
'-d'|'--depth')
if [[ "$2" =~ ^[0-9]+$ ]]; then
max="-maxdepth $2"
shift 2
else
echo " >> Parameter for --depth / -d needs to be a positive integer!" >&2
exit 2
fi
;;
'-r'|'--raw')
raw=1
shift
;;
'-h'|'--help')
echo "Syntax: $(basename "$0") [ --depth / -d <depth> ] [ --raw / -r ]"
exit
;;
'--')
shift
break
;;
esac
done
if [[ ! -z "$max" && $raw -eq 0 ]]; then
echo -e " >> Depth limit is set to $max.\n" >&2
fi
getExtension()
{
local res="${*##*/}"
res="${res:1}"
if [[ $res == *'.'* ]]; then
echo ".${res##*.}"
fi
}
inArray()
{
local i; for i in $result; do
[[ "$i" == "$*" ]] && return 0
done
return 1
}
while IFS= read -r -d '' file; do
ext="`getExtension "$file"`"
[[ -z "$ext" ]] && continue
inArray "$ext"
if [[ $? -ne 0 ]]; then
result="$result $ext"
let count=$count+1
fi
done < <(find $max -not -name . -not -name .. -print0)
result="${result:1}"
for i in $result; do
echo $i
done
[[ $raw -eq 0 ]] && echo -e "\n >> Found $count different extensions." >&2