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?
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?
Horrible hack, won't work with
-0
or any actions other than-print
, inefficient, etc. etc…Basically this just runs
until
find
returns non-zero status or prints nothing.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.It uses find to list all the files. The first awk command counts all the '/' characters. It sorts on the count and then drops the count column. Finally it uses xargs to grep the sorted list of files.
It is really ugly.
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.A breadth-first find using variable as its queue.
Create
bfs.sh
Make it executable:
$ chmod u+x ./bfs.sh
Then you can do a breadth-first find by:
$ ./bfs.sh /path/to/somewhere -name foobar