-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathffind
executable file
·211 lines (181 loc) · 5.51 KB
/
ffind
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
#!/usr/bin/env bash
#
# NAME
#
# ffind - friendly find wrapper
#
# SYNOPSIS
#
# ffind <pattern> [<path>...] [<expression>...]
#
# DESCRIPTION
#
# ffind is a friendly wrapper around find(1), which takes care of 90%
# of my find(1)-use cases. I wrote ffind primarily because I grew
# tired of piping the output of find(1) to grep(1).
#
# OPTIONS
#
# The first argument in the argument list must be a valid egrep(1)
# pattern to match full file paths and the rest of the arguments can
# be paths or find(1) expressions.
#
# In addition to standard find(1) expressions the option "-ns" may
# also be passed to disable "smartness". In that case, the pattern
# will be always matched case sensitively and all directories will be
# scanned.
#
# In case you do not want ffind to filter any results when more than
# one argument is supplied, use an empty string '' as the pattern (or
# even better, use find(1) instead).
#
# EXAMPLES
#
# o Search for PDF/PS files inside the subdirectory `physics' with
# the word `mechanics' in their names
#
# ffind 'physics\/.*mechanics.*\.(ps|pdf)$'
#
# o Recursively delete all temporary files (.~, .bak, .log, etc.)
# from the current directory
#
# ffind '\.(~|bak|log)$' -delete
#
# or
#
# ffind '\.(~|bak|log)$' -print0 | xargs -0 rm
#
# o Find all empty directories that start with an uppercase letter
#
# ffind '^[A-Z]' -type d -empty
#
# DEPENDENCIES
#
# GNU version of find(1).
#
# BUGS
#
# Yes.
#
[[ "$*" ]] || {
echo >&2 "usage: ${0##*/} <pattern> [<path>...] [<expression>...]"
exit 1
}
patt="$1"; shift
path=()
for arg
do
# The first argument starting with a `-' is assumed to
# begin the expression chain.
if [[ "$arg" =~ ^-.* ]]
then
break
elif [[ -d "$arg" ]]
then
path+=("$arg")
else
echo >&2 "${0##*/}: invalid path '$arg'; skipping"
fi
shift
done
# Disable smartness if -ns is passed as an argument.
smart="yes"
for arg
do
[[ "$arg" == "-ns" ]] && { smart="no"; shift; break; }
done
# Default find args. If find encounters a global option like -maxdepth
# after a test like -regex, then it will issue a warning; that's the
# reason why we're passing -nowarn.
args+=(
-L
-nowarn
-regextype egrep
)
if [[ "$smart" == "yes" ]]
then
args+=(
# Ignore standard VCS directories.
-not \(
# Bazaar
# http://bazaar.canonical.com/
-path '*/.bzr/*' -o
# Codeville
# http://freecode.com/projects/codeville
-path '*/.cdv/*' -o
# Interface Builder (Xcode)
# http://en.wikipedia.org/wiki/Interface_Builder
-path '*/~.dep/*' -o
-path '*/~.dot/*' -o
-path '*/~.nib/*' -o
-path '*/~.plst/*' -o
# Git
# http://git-scm.com/
-path '*/.git/*' -o
# Mercurial
# http://mercurial.selenic.com/
-path '*/.hg/*' -o
# quilt
# http://directory.fsf.org/wiki/Quilt
-path '*/.pc/*' -o
# Subversion
# http://subversion.tigris.org/
-path '*/.svn/*' -o
# Monotone
# http://www.monotone.ca/
-path '*/_MTN/*' -o
# CVS
# http://savannah.nongnu.org/projects/cvs
-path '*/CVS/*' -o
# RCS
# http://www.gnu.org/software/rcs/
-path '*/RCS/*' -o
# SCCS
# http://en.wikipedia.org/wiki/Source_Code_Control_System
-path '*/SCCS/*' -o
# darcs
# http://darcs.net/
-path '*/_darcs/*' -o
# Vault/Fortress
-path '*/_sgbak/*' -o
# autoconf
# http://www.gnu.org/software/autoconf/
-path '*/autom4te.cache/*' -o
# Perl module building
-path '*/blib/*' -o
-path '*/_build/*' -o
# Perl Devel::Cover module's output directory
# https://metacpan.org/release/Devel-Cover
-path '*/cover_db/*' -o
# Node modules created by npm
-path '*/node_modules/*' -o
# CMake cache
# http://www.cmake.org/
-path '*/CMakeFiles/*' -o
# Eclipse workspace folder
# http://eclipse.org/
-path '*/.metadata/*' -o
# Cabal (Haskell) sandboxes
# http://www.haskell.org/cabal/users-guide/installing-packages.html
-path '*/.cabal-sandbox/*' -o
# Python caches
# https://docs.python.org/3/tutorial/modules.html
-path '*/__pycache__/*' -o
-path '*/.pytest_cache/*' -o
# Filesystem crap
-path '*/__MACOSX/*' -o
-path '*/.Spotlight-V100/*' -o
-path '*/.Trash/*' -o
-path '*/.Trash-1000/*'
\)
)
# Smart regex: if the pattern contains at least one upper-case
# letter, assume that the user wants a case-sensitive search.
[[ "$patt" =~ [A-Z] ]] && args+=(-regex) || args+=(-iregex)
# Smart regex substitutions.
patt=$(sed 's|[\^]|/|' <<< "$patt")
else
args+=(-regex)
fi
args+=(".*(${patt}.*|${patt}$)")
exec find "${path[@]}" "${args[@]}" "$@"