Tell `ls` to sort by regular ASCII codes, not "intelligently"

I have a couple files and ls sorts them like this:

a
_b
c

but I want to have

_b
a
c

How can I do that?


Solution 1:

As a one-off command you can do this:

LC_COLLATE=C ls

Or you can add export LC_COLLATE="C" to your .bashrc to make it permanent (may have unexpected results sorting elsewhere).

More information on Ubuntu forums.

Solution 2:

Just in case there isn't a built-in way to do this, you could use a simple replacement for sort:

#!/usr/bin/env python

import sys

for i in sorted(sys.stdin):
    sys.stdout.write(i)

Save it, for example, at /bin/pysort and make it executable (sudo cp whatever.py /bin/pysort and sudo chmod a+x /bin/pysort), and run it as ls | pysort:

stefano@lenovo:~/t$ ls | pysort
_b
a
c

Solution 3:

would ls | sort not do exactly what you need?