Anyone who uses Terminal will run the ls command to get a listing of files and directories. It’s built in to macOS’ BSD Unix foundation layer. It has one key limitation for me: it has no option to list directories before listing files. Read on to learn how to deal with this issue.
gls in action in macOS’ Terminal app
The trick is to use Linux’s ls, aka Gnu ls, aka gls. This is part of coreutils, and this is easy to install using Homebrew:
| |
Once coreutils is installed, you can enter:
| |
Of course, you have to remember to call gls rather than ls, but that’s easy to sidestep by using an alias. Here‘s mine, which also forces the listing into long mode:
| |
Both forms of ls can be set to show colour output. The --color=auto option shown in the code above is equivalent to BSD ls’ -G but it gets its colour definitions from a different environment variable: LS_COLORS rather than LSCOLORS. LS_COLORS’ values are specified as style;foreground;background colour values. Each colour value is a specific code. For example:
| |
This sets directories (di) to cyan, links (ln) to yellow and executables (ex) to purple. The style value in each case is zero, which means ‘no style’. Change it, for example, to 1 for bold, or 5 for flashing, though you probably won‘t want to keep it that way. The complete list of styles is:
The foreground colours are:
And the backgrounds are:
All the colour options have defaults, most of which I left unchanged—I only altered the most commonly listed entities. The list of entities you can change is:
ls -lTo list the defaults, enter:
| |
As you’ll notice if you run the above command, a final tweak you can perform is to colour files by their extension. For example, to colour .md files white, you’d add:
:\*.md=0;37:
to your LS_COLORS. The scope for customisation is colossal, and the key here is to experiment and find the colours you prefer and which are clear against whatever window-background colour (and opacity) you’ve set Terminal to display.