15

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?

1 Answer 1

22

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.

10
  • Also I just noticed that using -depth clobbers the use of e.g. -name .git -prune to skip processing of the git folder... I wonder if there is another way to tell find to exclude some subfolders, which works despite -depth ?
    – Razzle
    Commented Jun 2, 2021 at 7:28
  • @Razzle -prune can’t work with -depth because the latter causes subdirectories to be processed first — so by the time -prune comes into play, the subdirectories and files which would be pruned have already been processed. The alternatives depend on your exact use-case, for example find . -depth \( -path './.git*' -prune \) -o -print skips .git and its contents. Perhaps you could ask a new question with the details of what you’re trying to do. Commented Jun 2, 2021 at 7:53
  • Seems to me you just showed me a way to use -depth while omitting .git and its subfolders - Bravo! (If it works when I test it :D ). But didn't you also just contradict yourself by demonstrating that prune can work with depth? (You just need to add the pruning clause in the brackets?) -Thanks!
    – Razzle
    Commented Jun 3, 2021 at 10:54
  • You lose the major feature of -prune which is to reduce the amount of processing required. The command above is effectively equivalent to find . -depth ! -path './.git*'. Commented Jun 3, 2021 at 11:07
  • Sadly find . -depth \( -path './.git*' -prune \) -o -print did not skip .git and its contents, when I just now tried it. HOWEVER the suggested equivalent command (tweaked a bit here: only dirs) find . -depth ! -path './.git*' -type d DID give me the folder tree (depth first listing) while excluding the .git contents. BRAVO and thanks! I have never really got to grips with negations in find paths. Must give them a closer look...
    – Razzle
    Commented Jun 3, 2021 at 15:31

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .