views:

236

answers:

3

Sometimes I know a file is not so deep away, but a very dense sub-directory does not allow me to find the files I want easily.

Can find (or any other tool) look for files using breadth-first search?

+1  A: 

Use find with the --maxdepth option.

That is at the Directories section in your reference page; might find other options more suitable depending on your needs.

To achieve exact breadth first searching, you will need to loop with mixed --mindepth and --maxdepth options. But, I don't think it is necessary to be that exact, a depth limited search will usually suffice.

nik
+4  A: 

Yes, sort of.

You can use the -depth option to make it process a directory's contents before the directory itself. You can also use the -maxdepth option to limit how many directories down it will drill.

unwind
"sort of" is right -- this still isn't a real breadth-first search, since a/b/c will be visited before a/d. Good enough for most purposes, though.
ephemient
A: 

Horrible hack, won't work with -0 or any actions other than -print, inefficient, etc. etc…

#!/bin/bash
i=0
while results=$(find -mindepth $i -maxdepth $i "$@") && [[ -n $results ]]; do
    echo "$results"
    ((i++))
done

Basically this just runs

find -mindepth 0 -maxdepth 0
find -mindepth 1 -maxdepth 1
find -mindepth 2 -maxdepth 2
…………………………………………………………………………

until find returns non-zero status or prints nothing.

ephemient