linuxbashgnu-findutils

Using find to find files WITH a certain pattern and not other pattern


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?


Solution

  • 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.