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?
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?
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.
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.
Jun 9, 2011 at 10:36
LC_COLLATE=C ls
does not work, but LC_ALL=C ls
does.
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
~/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.
Jun 8, 2011 at 21:08
would ls | sort
not do exactly what you need?
As of 2024, the LC_COLLATE=C ls
option works perfectly for me on Ubuntu 22.04 with sort (GNU coreutils) 8.32 and LANG=en_GB.UTF-8
. However, in case this does not work on your platform/locale, you could do this fairly easily with perl:
ls | perl -e '@f=sort <>; print @f'
I was also surprised that I couldn't find a relevant option in the sort
man page.