[Top][All Lists]

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: find 'breadth first' option

From: Bernhard Voelker
Subject: Re: find 'breadth first' option
Date: Wed, 16 Oct 2019 18:14:13 +0200
User-agent: Mozilla/5.0 (X11; Linux x86_64; rv:68.0) Gecko/20100101 Thunderbird/68.1.1

[adding the 'bug-findutils' ML]

On 10/16/19 4:54 PM, address@hidden 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 :-)

Hello Francois,

no, find(1) processes the directory hierarchy as provided by gnulib's FTS 
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,

reply via email to

[Prev in Thread] Current Thread [Next in Thread]