Logo
Stanislav Dakov Software developer, writer, and builder

Software Developer ยท Problem solver ยท Dad ยท Adventurer
Find me on:


Example of files:

pom-3.2.1.10-RELEASE.xml
pom-3.2.1.8-RELEASE.xml
pom-3.2.1.9-RELEASE.xml
pom-3.2.3-RELEASE.xml
pom-3.2.4-RELEASE.xml
pom-3.3.0-RELEASE.xml
pom-3.3.1-RELEASE.xml
pom-3.3.10-RELEASE.xml
pom-3.3.9-RELEASE.xml

๐Ÿ”—Sort by versions:

We can use 'sort -V'

โžœ  tmp git:(master) โœ— ls -1 *-RELEASE.xml | sort -V             
pom-3.2.1.8-RELEASE.xml
pom-3.2.1.9-RELEASE.xml
pom-3.2.1.10-RELEASE.xml
pom-3.2.3-RELEASE.xml
pom-3.2.4-RELEASE.xml
pom-3.3.0-RELEASE.xml
pom-3.3.1-RELEASE.xml
pom-3.3.9-RELEASE.xml
pom-3.3.10-RELEASE.xml

For reverse order we can use '-r'

โžœ  tmp git:(master) โœ— ls -1 *-RELEASE.xml | sort -Vr
pom-3.3.10-RELEASE.xml
pom-3.3.9-RELEASE.xml
pom-3.3.1-RELEASE.xml
pom-3.3.0-RELEASE.xml
pom-3.2.4-RELEASE.xml
pom-3.2.3-RELEASE.xml
pom-3.2.1.10-RELEASE.xml
pom-3.2.1.9-RELEASE.xml
pom-3.2.1.8-RELEASE.xml

To get last version file we can use 'tail -n 1'

โžœ  tmp git:(master) โœ— ls -1 *-RELEASE.xml | sort -V | tail -n 1 
pom-3.3.10-RELEASE.xml

We can use that value in bash script:

#!/bin/bash
last_version=$(ls -1 *-RELEASE.xml | sort -V | tail -n 1)

echo "Version: $last_version"