Is there any breadth-first/depth-first option in the Linux `find' utility?

link|improve this question

feedback

2 Answers

up vote 1 down vote accepted

There's nothing built-in to find, even GNU find. You can postprocess the output of find to sort by number of slashes, for example with Perl:

find ... | perl -e 'print sort {$a=~s!/!/! <=> $b=~s!/!/!} <>'
  • <> is the list of all input lines;
  • $a =~ s!/!/!g is the number of slashes in $a, which we use as the sort criterion.

If you can use zsh:

echo **/*(oe\''REPLY=${REPLY//[^\/]}'\')
  • **/* lists all files in the current directory and subdirectories.
  • The stuff inside the parentheses is a glob qualifier.
  • The glob qualifier oe controls the order in which matches are returned: they are sorted by the value of REPLY after running the code here in quotes for each match with REPLY initially set to the matched path.
  • Said code transforms $REPLY to delete everything except slashes. So the result consists of everything at depth 1 (empty resulting $REPLY), then everything at depth 2 ($REPLY ends up to be /), depth 3 (//), etc.
link|improve this answer
The sort post process is very interesting, however, sort needs the find to be completed, and you won't have chance to control break. – Xie Jilei Oct 1 '10 at 7:49
feedback

Nope

Go to this question on SO for workarounds.

link|improve this answer
feedback

Your Answer

 
or
required, but never shown

Not the answer you're looking for? Browse other questions tagged or ask your own question.