Search code examples
macosubuntucommand-linegnu-findutils

findutils 4.4.x on Ubuntu does not let me specify the `depth` option


Mission: List all direct decendants of a directory that are a directory itself.

On BSD (Mac OS), find . -type d -depth 1 works.

This is the output of Ubuntu 12.04 (GNU findutils 4.4.2):

$ find . -type d -depth 1
find: warning: you have specified the -depth option after a non-option argument -type,
but options are not positional (-depth affects tests specified before it as well as 
those specified after it).  Please specify options before other arguments.

find: paths must precede expression: 1
Usage: find [-H] [-L] [-P] [-Olevel] [-D help|tree|search|stat|rates|opt|exec] [path...] [expression]

Ok, next try:

$ find . -depth 1 -type d
find: paths must precede expression: 1
Usage: find [-H] [-L] [-P] [-Olevel] [-D help|tree|search|stat|rates|opt|exec] [path...] [expression]

Hrm, well, maybe it wants...

$ find -depth 1 . -type d
find: paths must precede expression: 1
Usage: find [-H] [-L] [-P] [-Olevel] [-D help|tree|search|stat|rates|opt|exec] [path...] [expression]

Apparently not, wtf, should it need...

$ find . -depth=1 -type d
find: unknown predicate `-depth=1'

Nah, this was obvious. So lets try as last resort...

$ find . -mindepth 1 -maxdepth 1 -type d
<my directories>

Yay, success! But, erm, why...?

And, as a bonus question, why is -mindepth 1 -maxdepth 1 so much faster than -depth 1 on BSD / OSX?


Solution

  • The -depth option does not take an argument:

    -depth Process each directory's contents before the directory itself.
    

    Options like -name, -type expect to be followed by something, this does not apply to -depth. It is more of a boolean option.