Monday, 18 January 2010

BASH: Changing ls colours

I have squinted at my dark blue directories on black background when doing an ls on my terminal for a long time and always meant to change it to something easier to read. As our network is down (again) today I spent half an hour figuring it out.

There are two methods as my mac and the linux cluster use different versions of ls.

On the Mac

The Mac (I am using 10.5) uses the environment variable LSCOLORS to change the colours used by ls when you use the --color option (which you should probably set as an alias in your .bash_profile file). You can look at info ls for all the options, but basically something along the lines of

export LSCOLORS="gxfxcxdxbxegedabagacad"

Will change the directories from blue to cyan. Much easier to read on a dark background terminal. All I changed was the fist letter from an e to a g, blue to cyan.

On Linux

I am using ls from coreutils version 5.2.1. This has a much more flexible system than the Mac, and uses a different environment variable, but it is similar.

The default colour configuration is in /etc/DIR_COLORS
which you can copy to your home using cp /etc/DIR_COLORS ~/.dir_colors. You can then edit this file to change the colours as you wish. The colour codes can be found here.
I just changed one line,

DIR 01;34 # directory
to
DIR 01;36 # directory

Which changes blue to cyan. Then you can just add this line to your .bash_profile

eval $(dircolors -b .dir_colors)

Which just runs a short shell script to generate the correct environment variable.

You could achieve the same thing with

export LS_COLORS='di=01;36'

But I quite like having a config file. I might play around a little with some of the other settings and see how it goes.

No comments:

Post a Comment