1

I'm trying to sort files (including hidden files) alphabetically in natural order in Ubuntu Xenial.

I got to this:

ls -l1va

Which prints:

.
..
.ICEauthority
.Private
.Xauthority
.atom
.babel.json
.bashrc
.bash_history
.bash_logout
.cache
.cargo
.config
.dbeaver4
.dbeaver-drivers
.dbus
.docker
.eclipse
.ecryptfs
.electron
.fltk
.gconf
.gem
.gimp-2.8
.gitconfig
.gksu.lock
.gnome
.gnome2
.gnome2_private
.gnupg
.local
.mozilla
.nano
.node-gyp
.npm
.nvm
.pki
.profile
.putty
.recently-used
.selected_editor
.ssh
.sudo_as_admin_successful
.thumbnails
.v8flags.6.2.414.32-node.8.4654dce123559e380233361202560f0e.json
.vmware
.vscode
.vscode-insiders
.wget-hsts
.xsession-errors
.xsession-errors.old
.yarn
.yarnrc
Desktop
Documents
Downloads
Music
Pictures
Projects
Public
Templates
Videos
bin
mnt
tmp

How can I get sorting to be case-insensitive and sort by letter instead of position in ASCII table (which I assume is what makes Z be before a)..?

ilkkachu
  • 133,243
  • 15
  • 236
  • 397
nkkollaw
  • 123
  • 1
  • 2
  • 11

4 Answers4

8

The sort order depends on the locale. The default C locale sorts as in your example:

$ LC_COLLATE=C ls -A
.hidden  Zappa  aardvark  vent  water  zebra

But many others give priority to the letters, and ignore the case and the leading periods:

$ LC_COLLATE=en_US.UTF-8 ls -A
aardvark  .hidden  vent  water  Zappa  zebra

(I wouldn't be surprised if some locales also considered characters like v and w as equal, but I can't come up with one that does that.)

Of course, you need to have those locales on the system. locale -a should show the locales that are currently usable on the system, and you can use locale-gen $localename to generate the ones you're missing. Then there's dpkg-reconfigure locales that shows a list where you can pick which locales to generate.

ilkkachu
  • 133,243
  • 15
  • 236
  • 397
2

Setting $LC_COLLATE to an appropriate value will collate the names appropriately.

LC_COLLATE=en_US ls -l1va

Additionally, it can be set in the shell's startup script in order to take effect each time.

export LC_COLLATE=en_US
Ignacio Vazquez-Abrams
  • 44,857
  • 7
  • 93
  • 100
1

As of Ubuntu 19.10, setting LC_COLLATE=C seems to be insufficient, but setting LC_ALL=C seems to work (such that ls will use ASCII-order for file and directory names).

Rayleigh
  • 805
  • 1
  • 6
  • 18
R. J. Mathar
  • 121
  • 4
0

The answer wasn't easy to find, but it was here on Stack Exchange.

The "fix" involves editing /usr/share/i18n/locales/iso14651_t1_common, and change the line

<U002E> IGNORE;IGNORE;IGNORE;<U002E> # 47 .

into:

<U002E> <RES-1>;IGNORE;IGNORE;<U002E> # 47 .

See: https://unix.stackexchange.com/a/361006/262190

nkkollaw
  • 123
  • 1
  • 2
  • 11