How to sort first directories then files etc… when using “ls” in Unix

I do so love *nix and love seeing the inventiveness that goes into some of these replies...

Mine's not nearly as fancy on GNU Linux :

alias ls='ls --color -h --group-directories-first'

Given that I'm more comfortable with my linux CLI apps, I tend to also update coreutils on OSX :

brew install coreutils
alias ls='/usr/local/bin/gls --color -h --group-directories-first'

The following command will list directories first, ordinary files second, and links third.

ls -la | grep "^d" && ls -la | grep "^-" && ls -la | grep "^l"

Also, it would make a great deal of sense to create an alias for this command to save keystrokes.

Edit:

If you want directories first, and then everything that is not a directory second, use this:

ls -la | grep "^d" && ls -la | grep -v "^d"


For the mac users, you can install coreutils.

This formula provides the GNU core utilities implementations, and for the commands that are also provided by macOS, they have been installed with the "g" prefix.

brew install coreutils
gls --color -h --group-directories-first

You can further simplify your life with an alias

alias ls='gls --color -h --group-directories-first'