Make ls command sort by file type (e.g. directories and file extension)
As you've found out, the Darwin version of the ls
command doesn't support the -X | --sort=extension
option. This option is unique to the GNU version of ls
.
You can obtain the GNU version of ls
for your system. The easiest way to do this is through the Homebrew package manager and porting system. With Homebrew installed do:
brew install coreutils
to add the GNU Core Utilities package to your system. All the utilities will be prefixed with g
so they don't collide with the default Darwin utils that OS X ships with.
With the GNU Core Utilites installed you can now do:
gls -X
And you'll see the output sorted as you desire.
If you want to use the GNU Core Utils instead of the utilities that come with OS X you can put the following line in your ~/.bashrc
file:
source /usr/local/Cellar/coreutils/8.14/aliases
And you won't need to use the g
prefix on the commands any more. I'll say this isn't an entirely safe thing to do as some of the GNU utils differ from the bash built-ins and Darwin tools in how they behave, enough so that they might screw up other things in your shell session. So do this with caution.
Based on an answer in the linked Super User article:
ls -l |sort -d -k 1.1,1.1r -k 9 | cut -c 50-
If you want to see the whole output of ls -l
, just leave out the cut
part.