Find Command Default Sorting Order – How to Use

findlinuxshell

what is the default sorting order for entries returned by the linux find command?

For example, if I issue

find . -type f -name '*mp3' 

and the output consists of multiple files across multiple sub-folders, what is the default order in which directories are listed? At the same time, what is the sorting order in which files within an individual directory are listed?

Sometimes it returns:

./B/01.mp3
./A/01.mp3
./A/04.mp3
./A/02.mp3

See how first the contents of directory B is listed, then that of directory A. At the same time within directory A, files are listed in a funny order.

Best Answer

find will be traversing the directory tree in the order items are stored within the directory entries. This will (mostly) be consistent from run to run, on the same machine and will essentially be "file/directory creation order" if there have been no deletes.

However, some file systems will re-order directory entries as part of compaction operations or when the size of the entry needs to be expanded, so there's always a small chance the "raw" order will change over time. If you want a consistent order, feed the output through an extra sorting stage.

Related Topic