In the manual for coreutils,

10.1.4 Details about version sort,

Version sorting handles the fact that file names frequently include indices or version numbers. Standard sorting usually does not produce the order that one expects because comparisons are made on a character-by-character basis. Version sorting is especially useful when browsing directories that contain many fi les with indices/version numbers in their names:

$ ls -1       $ ls -1v
abc.zml-1.gz  abc.zml-1.gz
abc.zml-12.gz abc.zml-2.gz
abc.zml-2.gz  abc.zml-12.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, which has some caveats worth noting.

LC_COLLATE is ignored, which means ls -v and sort -V will sort non-numeric prefixes as if the LC_COLLATE locale category was set to C.

• Some suffixes will not be matched by the regular expression mentioned above. Consequently these examples may not sort as you expect:

  1. Given a filename, what are the definitions of a prefix, version number, and suffix?
  2. For a suffix, in the regular expression ‘(\.[A-Za-z~][A-Za-z0-9~]*)*’,

    • what does ~ mean?
    • is part of the regular expression?

    Is the syntax of regular expressions introduced somewhere?

  3. In the three examples, what are the prefixes, version numbers and suffixes of the filenames?

  4. In the last example, the one in the last caveat, why are the filenames sorted that way by version sorting?



1 Answer 1


Granted, it's poorly explained.

Ultimate edification can only be achieved by reading the source code: https://github.com/gagern/gnulib/blob/master/lib/filevercmp.c. Basically:

The suffix is the longest string matching (\.[A-Za-z~][A-Za-z0-9~]*)*$. Note the $ which requires the match to be at the end of the filename. Dot-separated parts of the suffix are treated as version numbers for comparison purposes.

The ~ is just a character that may appear in a filename. This is sometimes used in version numbers like 3.4.1~alpha.

In the last example, the one in the last caveat, why are the filenames sorted that way by version sorting?

Because _ is not an allowed suffix character, the files that end in x86_64.rpm are treated as versionless (or just rpm considered as a version). It's not clear why they aren't sorted in dictionary order as a fallback though.

  • Thanks. What are the definitions of a prefix and a version number in a filename? In the filenames appearing in the three examples, what are their prefixes, version numbers, and suffixes?
    – Tim
    Dec 6, 2016 at 0:08

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.