MacOS – case insensitive `ls` for Catalina / APFS

command linemacosterminalzsh

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.

Best Answer

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.