Sorting files with version numbers

It can be quite annoying that when you perform a ls all your files are sorted alphabetically which give such result:
[johan@raskas versions]$ ls
a_file_name-1.0.1-10
a_file_name-1.0.1-11
a_file_name-1.0.1-2
a_file_name-1.0.1-3
a_file_name-1.0.1-4
a_file_name-1.0.1-9
some_other_file-2.0.10-1
some_other_file-2.0.9-1

Of course you want your file not sorted like that, but sorted per revision. Meaning that version 9 of the file comes before 10.
The ls-option –sort=version will do this for you.
The same files are now correctly sorted:
[johan@raskas-jhu versions]$ ls --sort=version
a_file_name-1.0.1-2
a_file_name-1.0.1-3
a_file_name-1.0.1-4
a_file_name-1.0.1-9
a_file_name-1.0.1-10
a_file_name-1.0.1-11
some_other_file-2.0.9-1
some_other_file-2.0.10-1

It is not rocket science, but it is a less known option which can come in very handy some times!

Leave a Reply