0

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
Community
  • 1
  • 1
XZS
  • 2,374
  • 2
  • 19
  • 38

1 Answers1

0

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; }
anubhava
  • 761,203
  • 64
  • 569
  • 643
  • A promising approach, but it does not yet work recursively. Subdirectories should also appear before files placed beside them. Chaining the two `find`s together via `-exec` could work, though. – XZS Jul 24 '16 at 14:59
  • On my gnu find subdirectories do appear before files of starting directory – anubhava Jul 24 '16 at 15:14
  • On my system, which is a GNU find over a tmpfs on kernel 4.6.4, `mkdir -p a/b/c`, `touch a/d` followed by `{ find . -depth -mindepth 1; find . -maxdepth 1 -depth -type f; } | tr '\n' ,` outputs `./a/d,./a/b/c,./a/b,./a,`. File `a/d` appears before sibling directory `a/b`. – XZS Jul 24 '16 at 15:30
  • Exactly same setup and same commands give me `./a/b/c,./a/b,./a/d,./a,` output. I am using `find (GNU findutils) 4.6.0` – anubhava Jul 24 '16 at 17:14
  • I tested same set of commands on Ubuntu with `find (GNU findutils) 4.4.2` and got same output i.e. `./a/b/c,./a/b,./a/d,./a,` – anubhava Jul 24 '16 at 17:29