The directory lister under UNIX is called ls
. In the deep and distant past this was a very black and white affair. GNU has changed all that by introducing an environment variable called LS_COLORS
which allows you to set the colours of files based on extension, permissions and file type.
As usual the instructions on how to configure it are locked away so that only a privileged few know how to configure them. When I started looking for this holy grail, I came across some poor guy who had worked it out through trial and error. I thought that there must be some documentation, somewhere on the internet. But, no! I have pieced together all the information I could find from mail list postings, support forums and when I eventually found it, the source code.
I will attempt to describe what I have discovered on my journey so that others may start from here with a better understanding of how LS_COLORS
works.
The BSD implementation of ls
used in BSD, FreeBSD and MacOS is slightly different. It uses a set or unset CLICOLOR
environment variable or the ls
command line switch -G
to indicate whether to use the colours defined in the environment variable LSCOLORS
. Whereas GNU's ls
uses the command line switch −−colour
and the environment variable LS_COLORS
instead (Thanks Gilles).
If you do not specify a colour to use then the default will be used, which is why unsetting LS_COLORS
has no visible effect. The defaults can be obtained by running the dircolors
command
dircolors --print-database
which gives you
LS_COLORS='rs=0:di=01;34:ln=01;36:mh=00:pi=40;33:so=01;35:do=01;35:bd=40;33;01:cd=40;33;01:or=40;31;01:su=37;41:sg=30;43:ca=30;41:tw=30;42:ow=34;42:st=37;44:ex=01;32:*.tar=01;31:*.tgz=01;31:*.arj=01;31:*.taz=01;31:*.lzh=01;31:*.lzma=01;31:*.tlz=01;31:*.txz=01;31:*.zip=01;31:*.z=01;31:*.Z=01;31:*.dz=01;31:*.gz=01;31:*.lz=01;31:*.xz=01;31:*.bz2=01;31:*.bz=01;31:*.tbz=01;31:*.tbz2=01;31:*.tz=01;31:*.deb=01;31:*.rpm=01;31:*.jar=01;31:*.rar=01;31:*.ace=01;31:*.zoo=01;31:*.cpio=01;31:*.7z=01;31:*.rz=01;31:*.jpg=01;35:*.jpeg=01;35:*.gif=01;35:*.bmp=01;35:*.pbm=01;35:*.pgm=01;35:*.ppm=01;35:*.tga=01;35:*.xbm=01;35:*.xpm=01;35:*.tif=01;35:*.tiff=01;35:*.png=01;35:*.svg=01;35:*.svgz=01;35:*.mng=01;35:*.pcx=01;35:*.mov=01;35:*.mpg=01;35:*.mpeg=01;35:*.m2v=01;35:*.mkv=01;35:*.ogm=01;35:*.mp4=01;35:*.m4v=01;35:*.mp4v=01;35:*.vob=01;35:*.qt=01;35:*.nuv=01;35:*.wmv=01;35:*.asf=01;35:*.rm=01;35:*.rmvb=01;35:*.flc=01;35:*.avi=01;35:*.fli=01;35:*.flv=01;35:*.gl=01;35:*.dl=01;35:*.xcf=01;35:*.xwd=01;35:*.yuv=01;35:*.cgm=01;35:*.emf=01;35:*.axv=01;35:*.anx=01;35:*.ogv=01;35:*.ogx=01;35:*.aac=00;36:*.au=00;36:*.flac=00;36:*.mid=00;36:*.midi=00;36:*.mka=00;36:*.mp3=00;36:*.mpc=00;36:*.ogg=00;36:*.ra=00;36:*.wav=00;36:*.axa=00;36:*.oga=00;36:*.spx=00;36:*.xspf=00;36:';
export LS_COLORS
The database in question is a globally readable file which can be overridden depending on your terminal type. The database file is initially set to /etc/DIR_COLORS
then overridden with one of the following. The first being the least precedent and the last being the most.
/etc/DIR_COLORS.$TERM
/etc/DIR_COLORS.$TERM
$HOME/.dircolors
$HOME/.dircolors.$TERM
$HOME/.dir_colors
$HOME/.dir_colors.$TERM
This configuration file has several options:
COLOR
- ether:- tty (add colour to
ls
when output is on the command line) - all (all colours to ls on the command line and when piping i.e.
ls > file.txt
will have colour coding information in it) - none (switch off)
- tty (add colour to
OPTIONS
- strictly not related toLS_COLORS
. This allows you to add extra options to thels
command.TERM
- one line per terminal type that supports colour.EIGHTBIT
- 1 (on) or 0 (off) to allow 8-bit output.- and finally the colour definitions for file type and file extensions.
On most systems the ls
command has been aliased to use the coloured option by default:
alias "ls=ls --color=auto"
The LS_COLORS
environment variable is a colon separated list of key=colour pairs. There are 2 types of key: file types and file extensions.
Below is a list of all the keys (that I know of!). Thanks to Bartman for his starter page.
Key | /etc/DIR_COLORS name | Notes |
---|---|---|
no | NORMAL, NORM | Global default, although everything should be something |
fi | FILE | Normal file |
di | DIR | Directory |
ln | SYMLINK, LINK, LNK | Symbolic link. If you set this to 'target' instead of a numerical value, the colour is as for the file pointed to. |
pi | FIFO, PIPE | Named pipe |
do | DOOR | Door |
bd | BLOCK, BLK | Block device |
cd | CHAR, CHR | Character device |
or | ORPHAN | Symbolic link pointing to a non-existent file |
so | SOCK | Socket |
su | SETUID | File that is setuid (u+s) |
sg | SETGID | File that is setgid (g+s) |
tw | STICKY_OTHER_WRITABLE | Directory that is sticky and other-writable (+t,o+w) |
ow | OTHER_WRITABLE | Directory that is other-writable (o+w) and not sticky |
st | STICKY | Directory with the sticky bit set (+t) and not other-writable |
ex | EXEC | Executable file (i.e. has 'x' set in permissions) |
mi | MISSING | Non-existent file pointed to by a symbolic link (visible when you type ls -l) |
lc | LEFTCODE, LEFT | Opening terminal code |
rc | RIGHTCODE, RIGHT | Closing terminal code |
ec | ENDCODE, END | Non-filename text |
*.extension | Every file using this extension e.g. *.jpg |
The keys (above) are assigned a colour pattern which is a semi-colon separated list of colour codes.
Effects
Code | Property |
---|---|
00 | Default colour |
01 | Bold |
04 | Underlined |
05 | Flashing text |
07 | Reversetd |
08 | Concealed |
Colours
Code | Property |
---|---|
30 | Black |
31 | Red |
32 | Green |
33 | Orange |
34 | Blue |
35 | Purple |
36 | Cyan |
37 | Grey |
Backgrounds
Code | Property |
---|---|
40 | Black background |
41 | Red background |
42 | Green background |
43 | Orange background |
44 | Blue background |
45 | Purple background |
46 | Cyan background |
47 | Grey background |
Extra colours
Code | Property |
---|---|
90 | Dark grey |
91 | Light red |
92 | Light green |
93 | Yellow |
94 | Light blue |
95 | Light purple |
96 | Turquoise |
97 | White |
100 | Dark grey background |
101 | Light red background |
102 | Light green background |
103 | Yellow background |
104 | Light blue background |
105 | Light purple background |
106 | Turquoise background |
107 | White background |
Most of the defaults are all right and not really worth changing. By default, executable directories show up as white on a light green background and normal directories which are blue on a black background. Both of these settings make your eyes go funny! So here is how to change just them.
LS_COLORS="ow=01;90:di=01;90"
export LS_COLORS
The above snippet changes both directories colours to grey.
Just as an aside, the easiest way to remap your colours is via the client control interface. I use PuTTY which allows you to redefine the terminal colour settings, but I think I'll save that for another block entry!