40

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?

Emil
  • 2,209
  • If it helps: http://apple.stackexchange.com/a/22304/8546 observes that HFS Plus is usually configured to be case insensitive but case preserving. – Graham Perrin Feb 14 '13 at 19:43
  • 1
    Note for Googlers: macOS sort and ls also respects LC_COLLATE, but the LC_COLLATE definition for locales like en_US.UTF-8 is just a symlink that makes it behave like C locale. See mike's answer below for more details. – SpinUp __ A Davis Jul 14 '22 at 00:45

10 Answers10

20

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
Sairam
  • 5,736
  • 2
    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 '13 at 13:42
  • Recent Googlers note: ls and sort do indeed respect LC_COLLATE, but the definitions for LC_COLLATE are different in BSD/macOS compared to Linux. See the answer, which currently has many fewer votes. – SpinUp __ A Davis Jul 13 '22 at 19:48
6

Update as of Aug 1, 2021:

ls -alFG

Explanations:

-a      Include directory entries whose names begin with a dot (.).
-l      (The lowercase letter ``ell''.)  List in long format.  (See below.)  A total sum for all the file sizes is output on a line before the long
        listing.
-F      Display a slash (`/') immediately after each pathname that is a directory, an asterisk (`*') after each that is executable, an at sign (`@')
        after each symbolic link, an equals sign (`=') after each socket, a percent sign (`%') after each whiteout, and a vertical bar (`|') after
        each that is a FIFO.
-G      Enable colorized output.  This option is equivalent to defining CLICOLOR in the environment.  (See below.)

Old:

I know this has been answered but this work best for me:

ls -f1 -alFG

It lists all details and sorts them by ignoring case.

Jared Burrows
  • 173
  • 1
  • 7
  • 3
    This doesn't seem to work with macOS 11.4 anymore (I get a truly unsorted listing probably reflecting the order in the filesystem). – cbrnr Jul 30 '21 at 09:43
  • @cbrnr Why downvote? I wrote this answer 7+ years ago. If you have a suggestion, suggest it. – Jared Burrows Aug 01 '21 at 19:08
  • 1
    I downvoted because your answer doesn't work anymore on macOS 11.4. However, the title specifically mentions OS X Lion, so I removed my downvote because it might still work there. The best solution on 11.4 seems to be coreutils and gls. – cbrnr Aug 02 '21 at 05:40
5

This has been bugging me for awhile now, and I finally got it sorted (heh). After trying a bunch of suggestions that didn't work, here's what did.

If you're willing to install MacPorts (or Homebrew, or Fink), the GNU version of ls does exactly what you want. I use MacPorts, myself, so that's the approach I'll explain:

  1. Download and install MacPorts:

    http://www.macports.org

  2. Install the GNU Coreutils package:

    sudo port install coreutils

  3. You should now have GNU ls: gls. Try it in a directory that contains items that start with both uppercase and lowercase letters:

    gls -U

    (The -U option actually means "unsorted", but on OS X that has the desired effect of making it case insensitive.)

  4. Add this alias in your .bash_profile so the regular ls will work the way you want it to (I like the color output, but you can omit that if you want; you only need the -U):

    alias ls='gls -U --color'

Note that the -U option probably won't work on other platforms. In OS X, it always seems to do the right thing (maybe because HFS+ is effectively case-insensitive -- "case-aware", technically), but if you try it on a Linux box, the results will most likely just not be sorted at all.

  • 1
    Yep…doesn't look like there's any way to do this with APFS. – Jason Sims Dec 23 '17 at 20:28
  • Confirm does work on 10.12 though. Thanks! – Colonel Panic May 01 '18 at 17:02