Hai’s Blog

April 25, 2009

Changing Directory Listing (ls) Color in Linux and Mac OS X

Filed under: Unix — Hai @ 6:53 am

Problem

I practically “live” on the Linux command line and one thing that has been bugging me for so long that now I decided to do something about it. On my company’s Linux systems the ls command produces color output, which makes it easy to distinguish different file types. However, the directories are dark blue which makes it nearly impossible to see against the black background.

My Solution

To fix the problem, my research took me to the LS_COLORS environment variable and a command named dircolors. The solution is to set the LS_COLORS environment variable to control how the ls command chooses its color. I can set the LS_COLORS variable manually, but there is a better way which employes the dircolors command.

The dircolors command output commands to set the LS_COLORS environment. It also output the colors in human-readable format, allowing easy modifications.

To use the dircolors, the first step is to save the current settings into a file. From the terminal, I issued one of the following commands:

    dircolors -p > ~/dircolors.txt

The next step is to edit the file ~/dircolors.txt. This file’s format is easy to understand and self-documented; I had no problem finding the file that begins with “DIR” and change the color to my taste.

Next, I try out the new color scheme:

    eval $( dircolors -b ~/dircolors.txt ); ls # bash shell syntax
    eval `dircolors -b ~/dircolors.txt`; ls # C shell syntax

After finding the color scheme I liked, I saved it to my start up file:

    dircolors -b ~/dircolors.txt >> ~/.profile # bash shell
    dircolors -b ~/dircolors.txt >> ~/.cshrc   # C shell

From this point on, I no longer have to put up with hard-to-see colors.

Solution for BSD Systems

On BSD systems, which includes the Mac OS X, the variable in question is LSCOLOR (note the lack of the underscore character). The format of this variable is different from the Linux’s LS_COLORS. The default is exfxcxdxbxegedabagacad. The value of this variable consists of pairs of characters; the first character is the code for foreground color, and the second is for background. Please consult the man page for ls on BSD for more information.

The Interactive Solution

While I like to do things the hard way to learn more about the inner-working of the OS, there is a more interactive way: point your browser here to an interactive online web application which assist you in setting the colors. After finding your desired color scheme, you still have to cut and paste it to your start up file.

Additional References

Google is your friend: do a search for LSCOLORS or LS_COLORS will result in more information that you ever care to read.

Else If in Popular Programming Languages

Filed under: Programming, Tcl — Hai @ 5:43 am

Problem Statement

I write program in many languages and the one thing I keep forgetting is how to spell “else if”. OK, stop laughing and think about it: each language spells “else if” differently: else if, elseif, elsif, or even elif.

Solution

To put my mind to rest, I create this list which serves as a quick look-up for me. I am sure that someone else might find this useful.

The Languages

Language Keywords Notes
bash if, then, elif, else, fi The fi keyword ends the if construct.
C/C++/C# if, else if, else Technically, these languages does not have the “else if” keyword, but the effect is the same.
Java/Javascript if, else if, else See note for C
Perl if, elsif, else
Python if, elif, else
Tcl if, elseif, else
Visual Basic If, Then, ElseIf, Else, End If Not case sensitive, but the IDE usually spells as presented

Blog at WordPress.com.