using find in bash to operate of filles based on their extension patterns
Unquoted *.txt
is bad. See find
utility does not output all files when using wildcards.
is it possible to combine these 4 commands into one?
Yes. See Combining multiple find
commands into one.
find . \( -name '*.txt' -o -name '*.xml' -o -name '*.pdbqt' \) …
(This uses 3 patterns because your 4th command duplicates the 1st one and there is no point in using the same pattern twice.)
Is it possible alternatively to use
find
in order to remove ALL files which are NOT belong to the*.dlg
extensions?
Yes (and I assume you want regular files, not all files):
find . -type f ! -name '*.dlg' # -delete
If the above command prints files you really want to delete then run it with #
removed.
The ! -name '*.dlg'
test is negated -name '*.dlg'
. It succeeds if and only if -name '*.dlg'
would fail.
You used -delete
in the question, so your find
apparently supports it. Users with less powerful find
see find
: -exec rm {} \;
vs. -delete
- why is the former widely recommended?
Note in your case bare find
works like find .
. I used find .
explicitly because some implementations of find
require at least one path in the command line.