Sort files with underscores first in Linux

On my work Linux box (Ubuntu 12.04, aka “Precise Pangolin”), the ‘ls’ command lists files beginning with an underscore intermixed with other files. In other words, the following four files will be displayed like so:

blanders@arya$ ls
a.txt  b.txt  _c.txt  d.txt

This is not what I want. I want files that begin with an underscore to be sorted first in the list. Fortunately, there’s an easy way to fix this. The sort order is controlled by the LC_COLLATE locale variable. By default, it’s set to:

blanders@arya$ locale | grep LC_COLLATE

If we override this to set LC_COLLATE to either ‘C’ or ‘POSIX’, Linux will do what I want:

blanders@arya$ LC_COLLATE=C ls
_c.txt  a.txt  b.txt  d.txt

I don’t want to override LC_COLLATE globally, since that may cause unexpected effects elsewhere, so let’s just override it for the ‘ls’ command:

alias ls='LC_COLLATE=C ls'

blanders@arya$ ls
_c.txt  a.txt  b.txt  d.txt