Tell me more ×
Ask Different is a question and answer site for power users of Apple hardware and software. It's 100% free, no registration required.

How can I make the ls command in Max OS X Lion sort files and directories similar to how Ubuntu Linux does (case-insensitive, directories NOT on top, dot files NOT on top)? Ideally I'd like to do this without piping output to another command such as sort.

For example, I want to see:

foo
Foobar
MyStuff/
.stuff/
test.txt

instead of:

.stuff
Foobar
MyStuff/
foo
test.txt

In Linux, ls sort order is controlled by the system's locale, specifically LC_COLLATE. When LC_COLLATE=en_US.UTF-8, ls will sort items like I want. When LC_COLLATE=C, ls will sort similar to OS X.

LC_COLLATE is set to en_US.UTF-8 in OS X, but ls still sorts the old POSIX way. Does anyone know how I can make this behave more like Linux?

share|improve this question
If it helps: apple.stackexchange.com/a/22304/8546 observes that HFS Plus is usually configured to be case insensitive but case preserving. – Graham Perrin Feb 14 at 19:43

migrated from serverfault.com Dec 18 '11 at 7:09

4 Answers

It might not be possible:

Taking a look at the source code for ls, it uses strcoll to sort the filenames, and so should respect LC_COLLATE.

Some postings online suggest that the locales in BSD (and Darwin/OS X) are somewhat broken compared to those in Linux. I wrote a quick sorting program of my own which explicitly set it's locale and tested it using both the en_US.UTF-8 and C locales on my machine (Mac OS 10.6.3) and a university machine (Linux, FC11?). While sorting works as expected on the linux machine, ("a B c" vs "B a c"), the mac always sorts them as "B a c".

Source: http://ask.metafilter.com/130292/CaseInsensitive-LS-on-Mac-OS-X

ORIGINAL ANSWER

This command does not sort dot files, but shows additional directory listings

ls -f1 

I got close to this:

.
..
.stuff
foo
Foobar
MyStuff
test.txt
share|improve this answer
Interesting that "disabling sort" with the -f option actually seems to sort it as expected. I assume this is the filesystem/HFS+ sorting entries with a more "natural" collation. – Gerry Feb 15 at 13:42

As a workaround you can use a function and an alias:

function lssorted() { /bin/ls "$@" | sort -f ;}
alias ls='lssorted'

This produces a case insensitively sorted output for the standard ls command.

share|improve this answer
1  
I have to note: this will fail if for example -l is used. Then ls produces another line "total X" at the beginning, which will also be sorted. – Arne Dec 22 '11 at 8:51

If you are mainly concerned with the sort order of the dot files, you can sort by extension: Use the ls command from the GNU Fileutils with the option --sort=extension. (You can install the GNU Fileutils e.g. through macports.)

share|improve this answer

Run:

$ type ls

You'll probably find that your ls command is aliased.

share|improve this answer
On linux: ls is /bin/ls. On OS X: ls is hashed (/bin/ls). Either way, even if I call /bin/ls directly it still doesn't sort according to LC_COLLATE. – blt04 Oct 13 '11 at 22:37
1  
That's surprising, I would have wagered that your ls command was aliased to include --group-directories-first. It may still be done somewhere else, just not an alias. – MikeyB Oct 13 '11 at 23:08

Your Answer

 
discard

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