1

I want to use find to find files with _101_ in the name and not .jpg or .wsq extensions, but I cannot get this to work.

I tried things like this:

find . -type f  -name '*_101_*' -o -not -name *.jpg -o -name *.wsq

but it doesn't work.

What am I doing wrong?

Benjamin W.
  • 46,058
  • 19
  • 106
  • 116
  • The way logical NOT combines with OR and AND can be unintuitive; I recommend learning about [De Morgan's laws](https://en.wikipedia.org/wiki/De_Morgan%27s_laws) (and see [this question](https://stackoverflow.com/questions/26337003/why-non-equality-check-of-one-variable-against-many-values-always-returns-true)). – Gordon Davisson Jun 15 '21 at 18:28

2 Answers2

3

Your attempt does "matches _101_, or does not end in .jpg, or does not end in .wsq". That'll match every single file, based on the two extensions alone, as a file can only have one.

You have to group differently:

find . -type f -name '*_101_*' -not -name '*.jpg' -not -name '*.wsq'

This applies all rules (logical AND is implied).

You also should quote the parameters to -name, or they might be expanded by the shell instead of find.

Benjamin W.
  • 46,058
  • 19
  • 106
  • 116
1

You need to use parenthesis (or @Benjamin's solution)

otherwise a or not b or c is evaluated as (a or not b) or c.

And you need and instead of or to filter only files that satisfy both conditions (pass both tests). a and not (b or c)

find -name '*_101_*' -not \( -name '*.jpg' -o -name '*.wsq' \)
mugiseyebrows
  • 4,138
  • 1
  • 14
  • 15