2

When I do a find the results are apparently in random order, shouldn't they be sorted by name?

  • 3
    They actually are sorted. Just not alphabetically, but by their place in the file tree... – mveroone Mar 21 '14 at 8:45
  • @Kwaio Are they sorted with inode numbers? – dspjm Mar 21 '14 at 9:39
  • I'm not familliar with inode assignment but I think not. Just imagine you're doing a deep-first search over a tree, going alphabetically as the second sorting order. that's what you get. – mveroone Mar 21 '14 at 10:00
5

To return sorted results, find would have to find everything before it could output anything. That would make things much, much slower. If you need the results sorted, you can easily sort them by piping the output of find to sort.

| improve this answer | |
  • 2
    Piping the output into a separate sorting tool (like but not necessarily sort) also allows for any number of possible sort algorithms to be used. If it was built into find then you'd be stuck with what find offers or still need that separate sorting tool. – a CVn Mar 21 '14 at 8:49
  • I actually know how to sort, but just curious why the result is not sorted, I think this need to investigate what method find use to find files. – dspjm Mar 21 '14 at 9:38
  • It would not need to sort everything. find would only have to sort each directory before iterating over its contents for the results to be sorted. / ls is slower than find simply because it does sort the directories after reading them but before listing them. / The cause is that on file systems with unordered indexes for directories, the entries read need not be returned in sorted order, unlike how DOS and its FAT did. – Dan D. Mar 21 '14 at 11:25

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.