--group-directories-first option for "ls" command

I am using both Ubuntu 16.04 and OS X.

alias ll='ls -Flh --group-directories-first'

This is an alias in my bashrc, but somehow --group-directories-first option got illegal in OS X after I upgraded my bash with Homebrew. In my Ubuntu desktop, I still can use that option.

In what version of Bash, the option is unsupported? The Bash version in my OS X machine says GNU bash, Version 4.4.12(1)-release (x86_64-apple-darwin15.6.0)

I would like to use --group-directories-first option again. Is there any way to achieve it?


Install coreutils with Homebrew and alias ll to gls -Flh --group-directories-first instead.

  • brew install coreutils installs GNU Coreutils, in case it is not installed.
  • Use alias ll='gls -Flh --group-directories-first' in .bashrc.
    (If you want to use the same .bashrc file in both operating systems, see below.)

In Ubuntu, ls is provided by GNU Coreutils, which Ubuntu always has. That's why ls supports --group-directories-first in Ubuntu. Probably you had been using the GNU Coreutils version of ls on macOS before, too, which Homebrew installs as gls but which can be made usable as ls in several ways.

ls is an external command, not a Bash builtin. Using a different version or build of Bash should not affect it. I'm not sure what happened when you upgraded Bash using Homebrew. Maybe more than Bash got upgraded too; maybe you had an ls shell function or alias defined in a global configuration file that was replaced; maybe you had a symbolic link ls that had pointed to gls and was overwritten; maybe you still have something like that but your $PATH has changed. Whatever happened, GNU Coreutils ls supports --group-directories-first, and switching to it (as I believe bmike is suggesting) should fix your problem.

If for some reason you want to use the exact same .bashrc file in both systems, there are a few possible approaches. You could create a symlink, wrapper script, shell function, or (because Bash alias expansion is nonrecursive) shell alias for gls in your Ubuntu system. But I suggest instead checking which OS is being used in .bashrc. Although you will get a different ll alias defined in each system, this approach has the benefit of being self-documenting. Your .bashrc will make sense to you in a year, or a month.

if [ "$OSTYPE" == linux-gnu ]; then  # Is this the Ubuntu system?
    alias ll='ls -Flh --group-directories-first'
else
    alias ll='gls -Flh --group-directories-first'
fi

Or if you find you usually prefer to run the Coreutils ls, even if you're not using the ll alias, you can make ls an alias to gls:

if [ "$OSTYPE" != linux-gnu ]; then  # Is this the macOS system?
    alias ls=gls
fi

alias ll='ls -Flh --group-directories-first'

(Thanks go to soroushjp for catching a mistake in an earlier version of that script.)