How to enable colorized output for the ls command in Mac OS X Terminal

in Categories , , , last updated January 19, 2016

I am new to Apple OS X. How do I enable colorized output for my ls command on Apple Mac OS X just like the ones that shown on my Ubuntu Linux server? How do I configure Mac OS X Terminal app to have color ls output?

You can enable colorized output by passing the -G option to ls command on Apple Mac OS X or FreeBSD operating system. You don’t have to install anything special. Just pass the -G option to the ls command.

Mac OS X color ls output option

Open the terminal application and simply type the following command:
$ ls -G
Sample outputs:

Fig,01: OS X ls command in action
Fig,01: OS X ls command in action

Configure ~/.bashrc to display colorful ls command

You need to add the following command in the ~/.bashrc file:
$ vi ~/.bashrc
Append the following alias:

alias ls='ls -G'
alias ll='ls -lG'

Save and close the file. Source the changes immediately:
$ source ~/.bashrc
Test it:
$ ls
$ ll

How do I skip colorful ls command output temporarily?

Simply run the ls command as follows:
$ \ls
$ \ls -l

The -G option is equivalent to defining CLICOLOR in the environment. The ls command supports the following environment variables:

  1. CLICOLOR – Use ANSI color sequences to distinguish file types.
  2. LSCOLORS – The value of this variable describes what color to use for which attribute when colors are enabled with CLICOLOR

Just type the following command to enable colorized output for the ls command:

export CLICOLOR=1
## better add to your ~/.bashrc ##
echo 'export CLICOLOR=1' >> ~/.bashrc

Understanding LSCOLORS values

You can define color for each attribute with the help of LSCOLORS, when colors are enabled with CLICOLOR. This string is a concatenation of pairs of the format fb, where f is the foreground color and b is the background color. The default value is:



ls AttributeForeground colorBackground color

The color and their code values are as follows:

CodeMeaning (Color)
hLight grey
ABold black, usually shows up as dark grey
BBold red
CBold green
DBold brown, usually shows up as yellow
EBold blue
FBold magenta
GBold cyan
HBold light grey; looks like bright white
xDefault foreground or background

You can now customize it as per your need in ~/.bashrc:

export CLICOLOR=1
export LSCOLORS=GxFxCxDxBxegedabagaced

Posted by: Vivek Gite

The author is the creator of nixCraft and a seasoned sysadmin and a trainer for the Linux operating system/Unix shell scripting. He has worked with global clients and in various industries, including IT, education, defense and space research, and the nonprofit sector. Follow him on Twitter, Facebook, Google+.

Share this on (or read 3 comments/add one below):

3 comment

  1. Thanks for this write-up.

    One problem I’m having is that after I follow all the steps and get my text color to change, when I open up a new iterm2 window I lose all the settings. Is there any way to save these CLICOLOR settings?

    Have a question? Post it on our forum!