[adding the 'bug-findutils' ML]

On 10/16/19 4:54 PM, francoisgr...@free.fr wrote:
Hello Mr Voelker,

I've been looking for a breadth-first search option for the find command, but 
found none. Is there anyone ?
If no, would it be possible to add it ?

Thank you :-)
Regards,
Francois

Hello Francois,

no, find(1) processes the directory hierarchy as provided by gnulib's FTS 
module,
or alternatively with -depth.

For the results of the search, this should not matter ... well, for the order
of the output, it obviously does.

If you would want to process each directory level first, and then go down,
then I suggest to use the "decorate-sort-undecorate" approach by printing
the current depth (%d), then sort by that, and finally remove the depth
from the input during the subsequent, actual processing, e.g.:

  $ find . -printf '%d %p\0' \
      | LC_ALL=C sort -z -k1,1n \
      | xargs -0 \
          sh -c '\
            while [ $# -gt 0 ]; do \
              d="${1%% *}"; \
              e="${1#* }"; \
              shift 1; \
              echo "processing (depth $d): entry: \"${e}\""; \
            done' sh
  processing (depth 0): entry: "."
  processing (depth 1): entry: "./a"
  processing (depth 2): entry: "./a/b"
  processing (depth 2): entry: "./a/f"
  processing (depth 3): entry: "./a/b/c"
  processing (depth 3): entry: "./a/b/e"
  processing (depth 3): entry: "./a/f/g"
  processing (depth 3): entry: "./a/f/h"

As the need for such breadth-first processing seems to be quite
seldom, and one can work around this with an approach like the
above, I currently wouldn't see a reason to add this (or another)
traversal algorithm to find(1).

Have a nice day,
Berny

Reply via email to