Does the -depth option of find imply depth-first tree traversal

find

As I understand it the -depth option of the find command causes the specified actions to take place on the way out of a directory (and maybe I understand it wrong) during a depth-first traversal of a tree structure.

Without the -depth option specified, does it normally make an action occur before the depth-first traversal is complete, or does it do a breadth-first traversal of the directories and run the action first normally?

Best Answer

find uses a depth-first strategy (as opposed to breadth-first), whether -depth is specified or not. -depth only guarantees that sub-directories are processed before their parents.

A quick example:

mkdir -p a/{1,2,3} b c
find .

produces

.
./a
./a/2
./a/1
./a/3
./b
./c

whereas

find . -depth

produces

./a/2
./a/1
./a/3
./a
./b
./c
.

If you want breadth-first search, you can use bfs which is a breadth-first implementation of find.

Related Question