Sorting in ls
depends on your locale settings, however, unfortunately this is not considered when using ls -v
and sort -V
.
Check LC_COLLATE="C" ls -l
and see the same "issue".
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’.
(via)
It means it will be sorted in ASCII order.
See also:
https://www.gnu.org/software/coreutils/manual/coreutils.html#Version-sort-ignores-locale
However, you could e.g. use python
to sort your versions:
Install natsort
module, e.g. if you have pip
installed:
pip3 install natsort
Put the following in a python script (e.g. sortv.py
):
#!/usr/bin/env python3
from natsort import humansorted
import sys
for line in humansorted(sys.stdin):
print(line.rstrip())
Then run ls -1f | python /path/to/sortv.py
.
This is nowhere perfect, it does not care about directories vs files, it should just show what you can do. Better would be to implement the whole thing in python
and not pipe from ls
which should never be parsed!
E.g.:
#!/usr/bin/env python3
import os
from natsort import humansorted
for (path, dirs, files) in os.walk('.'):
for d in dirs:
print('\033[94m{}\033[0m'.format(d))
for f in humansorted(f):
print(f)
break
--color=always
, but you might want to focus your question on the sorting (since|sort
didn't fix anything). – Jeff Schaller Feb 07 '22 at 14:28