Take the 2-minute tour ×
Ask Ubuntu is a question and answer site for Ubuntu users and developers. It's 100% free, no registration required.

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?

share|improve this question
2  
Might I ask why? –  RolandiXor Jun 8 '11 at 19:49
    
I use the underscore to denote special directories that need to go to top. In my projects folder, I have proj_a, proj_b and _old_projects. It is no use if this folder is within the o-projects. –  queueoverflow Jun 8 '11 at 21:10
add comment

3 Answers

up vote 8 down vote accepted

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.

share|improve this answer
1  
I guess I will just alias ls with that, so that it does not have side effects. –  queueoverflow Jun 8 '11 at 21:05
    
This doesn't work for me. I get this -> paste.pocoo.org/show/403333 (it's german but it shouldn't matter) –  dAnjou Jun 9 '11 at 9:55
2  
@dAnjou LC_COLLATE=C ls sets the traditional POSIX sorting order, which is by ASCII in which uppercase comes before underscore. To have underscores sort before uppercase you'd have to get under the bonnet and create a custom locale definition under /usr/share/i18n/locales. –  misterben Jun 9 '11 at 10:36
    
This may have worked in the past, but it's not working on Ubuntu 12.04. –  Mikel Jun 1 '12 at 19:18
add comment

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
share|improve this answer
1  
I would probably put it in ~/bin but nice fallback. It is probably better to use globbing within python since the output of ls might be strange if a filename contains a newline. –  queueoverflow Jun 8 '11 at 21:08
add comment

would ls | sort not do exactly what you need?

share|improve this answer
1  
no, that sorts it as "a _b c" –  Stefano Palazzo Jun 8 '11 at 19:55
1  
no! and sort also has not got an option for this :-) –  Rinzwind Jun 8 '11 at 19:55
    
It is strange that sort has so many special sorting ways but not the "use ASCII value" one … –  queueoverflow Jun 8 '11 at 21:07
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.