-
Notifications
You must be signed in to change notification settings - Fork 0
/
finder
105 lines (91 loc) · 2.23 KB
/
finder
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
#!/bin/bash
# This is a bash script for search files by regex string.
# It also can search files in archives.
# https://github.com/va1m/shv-file-finder
#file regex
fileregex="$1"
export fileregex
#true/false (optional)
includearch=false
export includearch
#text to search (optional)
text=
export text
#For zip listing lines
removeextrainfo() {
line="$1"
line=$(echo "$line" | grep -Po '\d+:\d+\s+(.+)' | cut -d' ' -f2-)
echo $line
}
export -f removeextrainfo
# Callback function for processing each file
callback() {
fullname="$1"
shortname=$(basename "$fullname")
[ "$(echo $shortname | grep -i -P $fileregex)" != "" ] && {
echo "$fullname"
}
[ "$includearch" == "true" ] && {
[ "$(echo $shortname | grep -P '.*\.(zip|jar|war)')" != "" ] && {
res=$(unzip -l "$fullname")
linenum=3 #3 lines for table header. Skip it.
echo "$res" | while read fullnameinzip; do
if [ $linenum == 0 ]; then
if [[ $fullnameinzip == '--------- '* ]]; then
#End of listing
return
fi
fullnameinzip=$(removeextrainfo "$fullnameinzip")
res=$(basename "$fullnameinzip" | grep -i -P "$fileregex")
[ "$res" != "" ] && {
echo "$fullname:"
echo " $fullnameinzip"
}
else
linenum=$(( $linenum - 1 ))
fi
done
return
}
[ "$(echo $shortname | grep -P '.*\.(tar\.gz|tgz)')" != "" ] && {
res="$(tar -tf $fullname)"
echo "$res" | while read fullnameinzip; do
res=$(basename "$fullnameinzip" | grep -i -P "$fileregex")
[ "$res" != "" ] && {
echo "$fullname:"
echo " $fullnameinzip"
}
done
}
}
}
export -f callback
parseparams() {
shift
for i in "$@"; do
case $i in
-a|--arc)
includearch=true
shift
;;
-t=*|--text=*)
text="${i#*=}"
shift # past argument=value
;;
*)
# unknown option
;;
esac
done
}
[ "$fileregex" == "" ] && {
echo "This is a script for search files by regex string in the current directory, subdirectories and archives."
echo ""
echo "Usage:"
echo "$(basename $0) regex [-a|--arc]"
echo "regex - A file name regex for search"
echo "-a --arc - Search in zip, jar, war, tar.gz, tgz archives. Optional."
exit 1
}
parseparams $@
find . -type f -exec bash -c 'callback "$0"' '{}' \; 2>&1 | grep -v 'Permission denied'