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)..?

4 Answers 4

9

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.

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
7
  • 1
    Yes, I tried that (I have it set to UTF-8.en_US or similar, and also tried your snippet) but it's not working.
    – nkkollaw
    Commented Nov 22, 2017 at 20:23
  • "or similar" isn't precise enough. What exactly is it set to? Commented Nov 22, 2017 at 20:24
  • Sure, it's en_US.UTF-8. Regarless, your command would override it, right?
    – nkkollaw
    Commented Nov 22, 2017 at 21:49
  • Yes, it would. The charset shouldn't matter in this case, since the filenames you list only use ASCII. Commented Nov 22, 2017 at 21:52
  • So, how can I sort alphabetically instead of by order in the ASCII table..?
    – nkkollaw
    Commented Nov 22, 2017 at 22:00
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).

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

3
  • heh, well, I can't say that your question made it clear that you wanted to keep the dotfiles first.
    – ilkkachu
    Commented Nov 22, 2017 at 22:48
  • Well, alphabetical order..? Special characters are often (always?) first.
    – nkkollaw
    Commented Nov 22, 2017 at 22:52
  • . is punctuation, it's not part of any alphabet. You'll likely find that dictionaries ignore punctuation (space ' -...) in first instance like they ignore case in first instance (but still deterministically sort ABC before (or after depending on the locale's convention) abc for instance. Commented Nov 28, 2023 at 11:29

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .