4

Ultimately, I wanted to set the latest version as an environment variable, so supplementing the selected answer (which provided me the correct sorting):

export LATEST_VERSION=$(printf '%s\n' * | sort -rV | head -1)

I have a directory with the following directory names:

ls -1r .
2.0
1.8
16.1
16.0
15.5
15.0
14.5
14.1
14.0
1.3
1.2
1.1.5
1.1.3
1.1.2

I would like to sort them to get the latest release version:

ls -1r . | head -1
16.1

So the underlying order should look like this:

16.1
16.0
15.5
15.0
14.5
14.1
14.0
2.0
1.8
1.3
1.2
1.1.5
1.1.3
1.1.2

Any help would be greatly appreciated. Simpler the better, but I'm open to any solution. Thanks!

2 Answers2

14

Modern GNU sort offers a -V flag for sorting according to version number rules:

$ printf '%s\n' * | sort -rV
16.1
16.0
15.5
15.0
14.5
14.1
14.0
2.0
1.8
1.3
1.2
1.1.5
1.1.3
1.1.2

Note that the above does not use ls. As a general rule, you shouldn't parse the output of ls(1).

Documentation

From man sort:

-V, --version-sort
natural sort of (version) numbers within text

The above is from the GNU man page.

FreeBSD's sort also supports -V.

OSX

Apple's sort is a GNU sort from the year 2005 which predates support for the -V option. A work-around can be found here. (Hat tips: l'L'l, mklement0)

Community
  • 1
  • 1
John1024
  • 109,961
  • 14
  • 137
  • 171
  • 1
    In `OS X` it's actually a real pain in comparison; I asked a similar question a while back: [solution here](http://stackoverflow.com/q/35385802/499581). – l'L'l Jul 08 '16 at 22:38
  • @l'L'l Very good. Answer updated with a link to that question. – John1024 Jul 08 '16 at 22:45
  • 1
    @mklement0 Interesting. I usually expect Apple to use BSD tools where available but you are right: OSX sort is GNU. From the only Apple man page that I found online (10.9), it appears to date from 2005. I also found a man page for [FreeBSD sort](https://www.freebsd.org/cgi/man.cgi?query=sort&sektion=1) and it shows support for `-V`. – John1024 Jul 08 '16 at 23:20
  • 1
    @John1024: Yes, it is curious that the odd - usually ancient - GNU utility comes with OS X. I don't know what guides those decisions and whether they're technology- or license-related. It is unfortunate that OS X apparently keeps up with _neither_ the GNU nor the BSD world in terms of providing (reasonably) recent versions. – mklement0 Jul 08 '16 at 23:21
  • 1
    @mklement0 The explanation that I have heard was that Apple won't use GPL v3 software. I have no explanation for the failure to keep up with BSD releases. – John1024 Jul 08 '16 at 23:26
  • Wow, perfect! Thank you for your thorough answer. – Avery Michelle Dawn Jul 11 '16 at 15:59
-1

FWIW, I'm in Mac OS right now, and haven't booted over to linux to test. But, it looks like sort with the numeric option has the smarts to do it:

But, I have a directory with your subdirectories in it, and ls sorts it badly:

/Users/thedave/tmp $ ls -1r
2.0
16.1.12
16.1
16.0
15.5
15.0
14.5
14.1
14.0
1.8
1.3
1.2
1.1.5
1.1.3
1.1.2
1.1
1.0.1
1.0

sort(1) accepts '-n' for numeric and '-r' for reverse:

/Users/thedave/tmp $ ls | sort -rn
16.1.12
16.1
16.0
15.5
15.0
14.5
14.1
14.0
2.0
1.8
1.3
1.2
1.1.5
1.1.3
1.1.2
1.1
1.0.1
1.0
The Dave
  • 49
  • 1
  • 2
  • 2
    This will fail on `OS X` with numbers such as `1.13.1`, `1.11.10`, `14.15.10`, etc. – l'L'l Jul 09 '16 at 00:12
  • 2
    Sorting version numbers is fundamentally different from numerical sorting: numerically, `1.9` is greater than `1.10`, but in terms of version numbers, it is the opposite (leaving aside the fact that a floating-point interpretation of a version string breaks down fundamentally with _3_ or more version components). No `sort` implementation will ever sort version numbers correctly with an option geared towards numerical sorting. – mklement0 Jul 09 '16 at 02:58