Instead of hacking around and parsing
ls output—which is never a good idea—you could simply install the GNU Coreutils, which give you the GNU versions of
tail, and many more, which offer much more than the BSD counterparts.
The easiest way to do that would be to install Homebrew, and then run
brew install coreutils. Now, the GNU coreutils will be available prefixed with a
g, and you could run:
gls -l --group-directories-first
Or, you could even alias that in your
alias ls='gls --group-directories-first'