Does `find` support multiple `maxdepth` conditions

find

I want to execute a find search with multiple maxdepths, depending on the directory.

Is this possible? It seems that -maxdepth is "global", but I was curious if there is some workaround.

(I'm aware it's possible to execute two separate commands, but using one would be faster, and it would keep the calling code simpler)

Best Answer

You can sort of emulate it using -prune on different matching pathnames. For example, to match /etc to depth 1, and /lib to depth 2:

find /etc /lib/ \
    \( -regex '/etc/[^/]*/.*'       -prune \) \
 -o \( -regex '/lib/[^/]*/[^/]*/.*' -prune \) \
 -o -print

You need to be careful to add the last line to print or otherwise operate on the remaining files and directories.

Related Question