Terminal Equivalent of Finder Sort Order

findersortterminal

One of the features I like about the Finder is that it sorts numbers "correctly", i.e – a file named "Episode 10" will come after "Episode 9" in a list, rather than after "Episode 1".

However, I haven't been able to find a way to replicate this ordering on the command line with any of the basic tools such as ls, sort and so-on, though I may have just missed or misunderstood an option.

Is it possible to mimic the Finder's alphabetic sort order to sort numbers by value when encountered? If so, is there a means of doing this that is cross-platform (even partially)?

Best Answer

GNU ls has the rather useful -v option that will do exactly what you want. Unfortunately there's no similar option for OS X's BSD-based ls.

A quick solution is to do the following:

$ ls | sort -n -k1.5

That sorts the output starting at the 5th character of the first field.

The same question was asked at StackOverflow and there are a number of other possible solutions listed there (some are Linux-specific though).