How do I get different colors for directories, etc. in iTerm2?

Solution 1:

You have to do two things:

  • set the LSCOLORS environment variable
  • create an alias for ls so that it shows colors by default

In your ~/.bash_profile add the following:

export LSCOLORS="EHfxcxdxBxegecabagacad" 

alias ls='ls -lGH'        <-----This shows in list format, follow symlinks colorized

The the colors are set by each bit above; the first being foreground and the second being background. The first two characters refer to directories having a bold blue foreground and a light grey background.

However, there's a great online utility to see what each of the colors mean and look like in real time. It will even generate the "code" for you. (I am not affiliated with this at all). It will work in both MacOS/FreeBSD and Linux. Make sure you select the BSD option for macOS.

LSCOLORS Calculator


The order of the attributes are as follows:


1.   directory
2.   symbolic link
3.   socket
4.   pipe
5.   executable
6.   block special
7.   character special
8.   executable with setuid bit set
9.   executable with setgid bit set
10.   directory writable to others, with sticky bit
11.   directory writable to others, without sticky

The color designators are as follows:

a    black
b    red
c    green
d    brown
e    blue
f    magenta
g    cyan
h    light grey
A    bold black, usually shows up as dark grey
B    bold red
C    bold green
D    bold brown, usually shows up as yellow
E    bold blue
F    bold magenta
G    bold cyan
H    bold light grey; looks like bright white
x    default foreground or background

Solution 2:

Open bash_profile using command:

open ~/.bash_profile

and add the following lines:

export PS1="\[\033[36m\]\u\[\033[m\]@\[\033[32m\]\h:\[\033[33;1m\]\w\[\033[m\]\$ "
export CLICOLOR=1
export LSCOLORS=ExFxBxDxCxegedabagacad
alias ls='ls -GFh'

then source bash_profile using:

source ~/.bash_profile

Solution 3:

An alternative to LSCOLORS is GRC (the GeneRic Colouriser), which can be used with pretty much any command-line app, not just ls.

If you've got Homebrew installed, install grc with brew install grc - this will set up aliases automatically, including for ls. It comes with aliases and config files for many different commands, and it's (relatively) trivial to hack a config file using Python regular expressions for any command that grc doesn't currently cover (tmutil and launchctl, for example).

(I was going to add this to the question I flagged as a possible dupe, but with your edit it's probably more useful here!)