Understanding escaped parentheses in find Understanding escaped parentheses in find bash bash

Understanding escaped parentheses in find


find has the following operators listed in order of precedence (highest -> lowest)

  1. ()
  2. !|-not
  3. -a|-and
  4. -o|-or
  5. , (GNU only)

Note: All tests and actions have an implied -a linking each other

So if you are not using any operators, you don't have to worry about precedence. If you are just using not like in your case, you don't really have to worry about precedence either, since ! exp exp2 will get treated as (! exp) AND (exp2) as expected, due to ! having higher precedence than the implied and.

Example where precedence matters

> mkdir empty && cd empty && touch a && mkdir b> find -mindepth 1 -type f -name 'a' -or -name 'b'./a./b

The above got treated as find -mindepth 1 (-type f AND -name 'a') OR (-name 'b')

> find -mindepth 1 -type f \( -name 'a' -or -name 'b' \)./a

The above got treated as find -mindepth 1 (-type f) AND ( -name 'a' OR -name 'b')

Note: Options ( i.e. -mindepth, -noleaf, etc... ) are always true

Conclusion

The following two uses of find are exactly the same

  • find . -type d -mmin +5 \! -empty \( ! -iname ".*" \) | wc -l
  • find . -type d -mmin +5 \! -empty \! -iname ".*" | wc -l

Both get treated as

  • find . (-type d) AND (-mmin +5) AND (! -empty) AND (! -iname ".*") | wc -l