Thursday, March 18, 2010

Nerd Food: Getting ls to behave

ls appears to be sorting rather weirdly of late (well, since quite a few Ubuntu releases), ignoring upper and lower case, e.g.:

drwxr-xr-x 2 marco marco 4096 2010-03-17 00:12 cmake
-rw-r--r-- 1 marco marco 8708 2010-03-17 18:39 CMakeLists.txt
drwxr-xr-x 2 marco marco 4096 2010-03-17 15:17 config
drwxr-xr-x 4 marco marco 4096 2010-03-11 18:22 cpp
drwxr-xr-x 2 marco marco 4096 2010-03-17 16:24 doc
drwxr-xr-x 2 marco marco 4096 2010-03-12 01:39 glade
drwxr-xr-x 5 marco marco 4096 2010-03-17 18:42 output
-rw-r--r-- 1 marco marco 1124 2010-03-17 16:26 README.TXT
drwxr-xr-x 2 marco marco 4096 2010-03-11 18:22 scripts
drwxr-xr-x 3 marco marco 4096 2010-03-11 18:22 sql
drwxr-xr-x 3 marco marco 4096 2010-03-11 18:22 templates

This is utterly annoying. The main reason people use upper case filenames is precisely because they come up at the top of the listing. It appears the reason why is the locale, more precisely LC_COLLATE.

$ locale | grep LC_COLLATE
LC_COLLATE="en_GB.UTF-8"

By changing it to C, it all behaves as expected:

$ export LC_COLLATE=C
$ ls
-rw-r--r-- 1 marco marco 8708 2010-03-17 18:39 CMakeLists.txt
-rw-r--r-- 1 marco marco 1124 2010-03-17 16:26 README.TXT
drwxr-xr-x 2 marco marco 4096 2010-03-17 00:12 cmake
drwxr-xr-x 2 marco marco 4096 2010-03-17 15:17 config
drwxr-xr-x 4 marco marco 4096 2010-03-11 18:22 cpp
drwxr-xr-x 2 marco marco 4096 2010-03-17 16:24 doc
drwxr-xr-x 2 marco marco 4096 2010-03-12 01:39 glade
drwxr-xr-x 5 marco marco 4096 2010-03-17 18:42 output
drwxr-xr-x 2 marco marco 4096 2010-03-11 18:22 scripts
drwxr-xr-x 3 marco marco 4096 2010-03-11 18:22 sql
drwxr-xr-x 3 marco marco 4096 2010-03-11 18:22 templates

Much better! To make it permanent, add it to your .bashrc.

No comments: