Sample Header Ad - 728x90

How to sort the results of `find` alphabetically and topologically?

2 votes
2 answers
2353 views
Let's say I have the following files laid out like this:
$ tree
.
├── 01/
│   ├── example.txt
│   └── foobar.txt
├── 02/
│   └── example.txt
└── 03/
    ├── example.txt
    └── test.txt

3 directories, 5 files
I would like find . to print out the directories and files in order, topologically (that is directory names before their contents), and within the same directory, alphabetically. Instead, these are the results I get:
$ find .
.
./02
./02/example.txt
./01
./01/foobar.txt
./01/example.txt
./03
./03/test.txt
./03/example.txt
I would like the output to look like:
.
./01
./01/foobar.txt
./01/example.txt
./02
./02/example.txt
./03
./03/example.txt
./03/test.txt
I don't want to pipe to sort or to any other command, because I would like to process the lines one by one in order before the find command completes.
Asked by Flimm (4473 rep)
Aug 23, 2021, 03:04 PM
Last activity: Jul 19, 2025, 08:07 AM