| « Dancing girl optical illusion | Spam filtering enhancement to Horde accepted » |
Configuring LS_COLORS
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.
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
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.
| 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 color is as for the file pointed to. |
| pi | FIFO, PIPE | Named pipe |
| do | DOOR | Door |
| bd | BLOCK, BLK | Black 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 | |
| 00 | Default colour |
| 01 | Bold |
| 04 | Underlined |
| 05 | Flashing text |
| 07 | Reversetd |
| 08 | Concealed |
| Colours | |
| 31 | Red |
| 32 | Green |
| 33 | Orange |
| 34 | Blue |
| 35 | Purple |
| 36 | Cyan |
| 37 | Grey |
| Backgrounds | |
| 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 | |
| 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 |
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!
14 comments
http://dailypackage.fedorabook.com/index.php?/archives/109-Wednesday-Why-Colour-ls.html
1) the fact that anyone makes a comment is nice!
2) It's another link to LS_COLORS which is a bit of a complicated affair, so the more links the better.
Added
I know that I get about 400 page views (per month) of this article, but if I mention affair, will I get all the porn hunters looking here? Oh no, I've mentioned porn does that mean I'll get even more? I'll analyse the blog stats over the next couple of months and let you know... (in another blog!)
thanks for any help.
It's an interesting request, you may want to email the GNU team and add it as a feature request or bug.
Try here: GNU FileUtils Project
Sure enough, I tried them out and they all work! Awesome. (now I'm gonna waste a good chunk of time seeing how I can further customize all my syntax highlighting, LS_COLORS, zsh completion menu colors, etc etc...)
Where did you find out about these?
it's very useful way to change color.
LS_COLORS="ow=01;90:di=01;90"
export LS_COLORS
overwrites a possible existing configuration of LS_COLORS, you can juste substitute the existing definition with sed (stream editor) as follows :
LS_COLORS="`echo $LS_COLORS |sed 's/di=[0-9]*;[0-9]*/di=01;34/; s/ow=[0-9]*;[0-9]*/ow=01;34/'`"
export LS_COLORS
Regards