Is there a simple way to recursively find all files in a directory hierarchy, that do not end in a list of extensions? E.g. all files that are not *.dll or *.exe
UNIX/GNU find, powerful as it is, doesn't seem to have an exclude
mode (or I'm missing it), and I've always found it hard to use regular expressions to find things that don't match a particular expression.
I'm in a Windows environment (using the GnuWin32 port of most GNU tools), so I'm equally open for Windows-only solutions.
Other solutions on this page aren't desirable if you have a long list of extensions -- maintaining a long sequence of
-not -name 'this' -not -name 'that' -not -name 'other'
would be tedious and error-prone -- or if the search is programmatic and the list of extensions is built at runtime.For those situations, a solution that more clearly separates data (the list of extensions) and code (the parameters to
find
) may be desirable. Given a directory & file structure that looks like this:You can do something like this:
Which results in:
You can change the extensions list without changing the code block.
NOTE doesn't work with native OSX
find
- use gnu find instead.one more :-)
Unix find command reference
Linux/OS X:
Starting from the current directory, recursively find all files ending in .dll or .exe
Starting from the current directory, recursively find all files tha DON'T end in .dll or .exe
Notes:
(1) The P option on grep indicates that we are using the Perl style to write our regular expressions to be used in conjunction with the grep command. For the purpose of excecuting the grep command in conjunction with regular expressions, I find that the Perl style is the most powerful style around.
(2) The v option on grep instructs the shell to exclude any file that satisfies the regular expression
(3) The $ character at the end of say ".dell$" is a delimiter control character that tells the shell that the filename string ends with ".dll"
The first two -name options have no -print option, so they skipped. Everything else is printed.