2

I am trying to find a way to get either the stock macOS ls or (more likely) the GNU ls a.k.a. gls to show my files & folders in case insensitive order.

All of my efforts have resulted in lowercase letters being sorted after the uppercase letters.

⚠️ There are lots of outdated answers which no longer work!

This same question was asked on this forum in October 2011 (and in August 2013 on Stack Overflow), but those answers no longer work on macOS in 2020.

What I've tried that does not work:

  1. LC_COLLATE does not seem to have any effect. I have tried these variations:

    LC_COLLATE="en_US.UTF-8"
    
    LC_COLLATE="cs_CZ.ISO8859-2"
    
    LANG="en_US.UTF-8" LC_COLLATE="en_US.UTF-8"
    

    There was no noticeable effect at all.

  2. Apparently ls -f or gls -U used to work for HFS+, although the output was supposed to be "unsorted", but apparently somehow in HFS+ "unsorted" was ¿accidentally? ¿coincidentally? case-insensitive? I guess? Whatever the reason, it no longer works, presumably because we’re using APFS now.

  3. ”Just pipe it through sort -f!” was never really an answer, but someone always seems to suggest it anyway. I want to be able to use all of the other features of ls/gls including color and -C which doesn’t work if we have to pipe through another command.


Is there some hidden method out there that I might have missed? Or does this functionality is actually not possible? The latter would seem extremely odd to me, but stranger things have happened, I guess.

P.S. - I use zsh as my default shell anyway, so if there’s a way to replicate this with zsh that would be OK too.

Nimesh Neema
  • 51,809
TJ Luoma
  • 20,659

3 Answers3

3

If done properly the LC_COLLATE method works:

Example:

user@host ~ % export LC_COLLATE="cs_CZ.ISO8859-2" && ls -la
total 56
-rw-r--r--   1 user      staff      0 Mar  1 20:39 bin
drwx------+  4 user      staff    128 Jan 30 10:51 Desktop
drwx------+  3 user      staff     96 Oct  9 04:40 Documents
drwx------+  4 user      staff    128 Jan  4 21:11 Downloads
drwx------+ 61 user      staff   1952 Nov  2 10:29 Library
drwx------+  4 user      staff    128 Oct  9 18:17 Movies
drwx------+  3 user      staff     96 Oct  9 04:40 Music
drwx------+  5 user      staff    160 Oct 10 03:22 Pictures
drwxr-xr-x+  4 user      staff    128 Oct  9 04:40 Public
drwxr-xr-x+ 18 user      staff    576 Mar  1 20:40 .
-r--------   1 user      staff      7 Oct  9 04:43 .CFUserTextEncoding
drwxr-xr-x   3 user      staff     96 Oct 10 17:50 .config
-rw-r--r--@  1 user      staff  14340 Dec 26 22:14 .DS_Store
drwx------   6 user      staff    192 Nov 22 11:40 .ssh
drwx------   2 user      staff     64 Mar  1 20:35 .Trash
-rw-r--r--@  1 user      staff   2620 Nov 15 02:44 .zprofile
-rw-------@  1 user      staff   1544 Mar  1 20:40 .zsh_history
drwxr-xr-x   5 root      admin    160 Sep 29 22:22 ..

So simply add export LC_COLLATE="cs_CZ.ISO8859-2" to your .zprofile/.zshrc/... and restart Terminal.

If you use this locale you might experience some irregularities in shell outputs/history files etc.

Here (using mixed us_en/de_de locales im macOS) I got some irregular entries in the command history (i.e. .zsh_history) after entering German umlauts.


Probably the best idea then: create a new custom LC_COLLATE file.

klanomath
  • 66,391
  • 9
  • 130
  • 201
  • Curious. I did not get that result when I tried this in zsh but if I try it on an account without my .zshenv/.zshrc then it works. Will have to ask the zsh folks for some help figuring out why it's not working for me. Must be a conflict in one of my other settings. Thanks! – TJ Luoma Mar 02 '20 at 02:49
  • I would suggest an alias for ls instead of setting LC_COLLATE globally. – Thorbjørn Ravn Andersen Apr 23 '20 at 09:16
1

I agree that I wouldn't change the global LC_COLLATE. Just use it locally in your alias:

alias ls="LC_COLLATE=cs_CZ.ISO8859-2 /bin/ls"

Watch out for the undesirable effect of this particular LOCALE. Files that start with "ch", "CH", or "Ch" will be sorted between files that start with "H" and "i".

I would like to create my own custom file, I'm going to try out this: https://gist.github.com/shaunsauve/56c30ad45d1261e7164e1b32316ef7c1

jaume
  • 15,010
0

You can get there by using LC_COLLATE="cs_CZ.ISO8859-2".

But watch out for this curiosity regarding the differences between the stock 'ls' command on macOS 10.15.4 box, and gls (GNU ls from coreutils via brew):

λ export LC_COLLATE="cs_CZ.ISO8859-2" && ls -la

-rw-r--r--    1 storm  staff   695  8 nov 06:35 argv.json
drwxr-xr-x   61 storm  staff  1952 22 apr 19:02 extensions
drwxr-xr-x    4 storm  staff   128  8 nov 06:35 .
drwxr-xr-x+ 154 storm  staff  4928 23 apr 10:43 ..
λ export LC_COLLATE="cs_CZ.ISO8859g-2" && gls -la

total 4
drwxr-xr-x    4 storm staff  128 Nov  8 06:35 .
drwxr-xr-x+ 154 storm staff 4928 Apr 23 10:43 ..
-rw-r--r--    1 storm staff  695 Nov  8 06:35 argv.json
drwxr-xr-x   61 storm staff 1952 Apr 22 19:02 extensions

Not sure how ls and gls end up sorting . and .. differently based on the same locale lexer info, but it does.

nohillside
  • 100,768