Next: , Previous: Sorting the output, Up: ls invocation


10.1.4 More details about version sort

The version sort takes into account the fact that file names frequently include indices or version numbers. Standard sorting functions usually do not produce the ordering that people expect because comparisons are made on a character-by-character basis. The version sort addresses this problem, and is especially useful when browsing directories that contain many files with indices/version numbers in their names:

     $ ls -1            $ ls -1v
     foo.zml-1.gz       foo.zml-1.gz
     foo.zml-100.gz     foo.zml-2.gz
     foo.zml-12.gz      foo.zml-6.gz
     foo.zml-13.gz      foo.zml-12.gz
     foo.zml-2.gz       foo.zml-13.gz
     foo.zml-25.gz      foo.zml-25.gz
     foo.zml-6.gz       foo.zml-100.gz

Version-sorted strings are compared such that if ver1 and ver2 are version numbers and prefix and suffix (suffix matching the regular expression ‘(\.[A-Za-z~][A-Za-z0-9~]*)*’) are strings then ver1 < ver2 implies that the name composed of “prefix ver1 suffix” sorts before “prefix ver2 suffix”.

Note also that leading zeros of numeric parts are ignored:

     $ ls -1            $ ls -1v
     abc-1.007.tgz      abc-1.01a.tgz
     abc-1.012b.tgz     abc-1.007.tgz
     abc-1.01a.tgz      abc-1.012b.tgz

This functionality is implemented using gnulib's filevercmp function. One result of that implementation decision is that ‘ls -v’ and ‘sort -V’ do not use the locale category, LC_COLLATE, which means non-numeric prefixes are sorted as if LC_COLLATE were set to ‘C’.