3

I am wondering if there is a way to run the find command and make it go through the directories (recursively) in alphabetical order (or in reverse)? I have a bunch of directories with names representing some order and often I need to find the earliest (or the last) based on the directory name.

There are questions and answers on how sorting the results returned by find (see below). However, I am interested to know if there is a way to make find to run the search alphabetically because it would be much more efficient than going through the whole thing and then sort.

Ideally, I can specify the command to stop after n hits are found, but that's secondary because I can also stop the command manually.

How do I get files found by command-line 'find' ordered by modification date in OS X? https://unix.stackexchange.com/questions/34325/sorting-the-output-of-find

2 Answers 2

1

find's directory listing order is given by the order of the entries stored in the filesystem.

You could consider writing a bash script that goes into each directory and lists the needed files.

0

You could try:

find -type d -maxdepth 1 | sort -h | xargs find

find -type d -maxdepth 1 lists directories, without recursing. We pipe the output into sort -h, to sort the results in a human-centric way. The sorted list of directories gets sent to xargs which uses them as command-line arguments for the final find, the one that does the majority of the work.

I know you wanted to avoid sort, but AFAIK, find is going to go through the file system by the filesystem's order.

Hope this helps.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .