Ubuntu: How to select a directory with the highest version?



Question:

Let's say I have three directories within my current working directory. Each one refers to a different version of a library/package. I would like to return only the directory with the highest version. For example:

program program-1.0 program-2.0  

The best I have been able to do is just with the find command.

find . -maxdepth 1 -type d -name "program*" -print  

But this provides all three directories. I would like the full path of the program2 directory.


Solution:1

Here's one way of doing it with find, sort and tail:

find "$PWD" -maxdepth 1 -type d -name "program*" | sort -V | tail -1  

This will also print the path.


Solution:2

Newer versions of the GNU sort utility have an option to sort based on version number: from man sort

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

So you could do

find . -maxdepth 1 -type d -name 'program*' | sort -V | tail -1  

Note:If u also have question or solution just comment us below or mail us on toontricks1994@gmail.com
Previous
Next Post »