12
1
Is there any breadth-first/depth-first option in the Linux `find' utility?
12
1
Is there any breadth-first/depth-first option in the Linux `find' utility?
5
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.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.$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.2
Nope
Go to this question on SO for workarounds.
1
My feeling is that you can. It involves grep and such and a loop, but I find it works very well, specifically for your case about the find not needing to be completed.
It is more resource intensive because of:
This is good because:
#!/bin/bash depth=0 while find -mindepth $depth -maxdepth $depth | grep '.' do depth=$((depth + 1)) done
You can also fit it onto one line fairly(?) easily:
depth=0; while find -mindepth $depth -maxdepth $depth | grep --color=never '.'; do depth=$((depth + 1)); done
But I prefer small scripts over typing...
1The sort post process is very interesting, however, sort needs the find to be completed, and you won't have chance to control break. – Xiè Jìléi – 2010-10-01T07:49:08.790