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)?

1 Answer 1


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).

  • wow...I did not know that :)
    – Ruskes
    Commented May 8, 2014 at 11:09
  • Thanks for this! It's a shame there isn't something more generic I could use, but I may be able to use the -k option combined with a quick sampling of files to try and configure it automatically; unfortunately I'm not always working with consistent formats, but I'm trying to make it automatic =)
    – Haravikk
    Commented May 9, 2014 at 12:40
  • @Haravikk You can always install GNU Coreutils and use the ls from there - probably easier than trying to fiddle with various sort parameters! It's easily installable from all of the third-party UNIX software installers - pkgsrc, MacPorts, Homebrew.
    – mjturner
    Commented May 9, 2014 at 12:44
  • The -k option is about fields, not columns. A field is separated by whitespace, and a different separator can be specified with -t. So ls | sort -n -k 2 -t '_' sorts a list with names like "prefix_12345" and "prefix_1234".
    – doekman
    Commented Jul 1, 2016 at 10:17

You must log in to answer this question.

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