directory-first-search with find

77 Views Asked by At

When find iterates directories, they show up in the order the VFS yields them. Can this order be changed to first traverse directories before looking at files placed beside them?

The -depth option is not the solution. It only changes

$ find
.
./afile
./directory
./directory/athirdfile
./other-directory

to

$ find -depth
./afile
./directory/athirdfile
./directory
./other-directory

(Note how only the second and third output line swapped places.)

This question instead seeks for a way to produce the following order.

./directory/athirdfile
./directory
./other-directory
./afile
1

There are 1 best solutions below

5
On

You can get your customized output by using 2 find commands. First find will get everything using -depth except files in current directory and 2nd find will get just the files from current level.

{ find . -depth -mindepth 1; find . -maxdepth 1 -depth -type f; }