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


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 -c ~/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 -c ~/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.


15 thoughts on “Changing Directory Listing (ls) Color in Linux and Mac OS X

  1. nathan

    I believe your C shell syntax should read as follows:

    dircolors -c ~/dircolors.txt >> ~/.cshrc # C shell

    when I tried your code, -b caused it to output a bunch of bourne shell stuff that my shell didn’t like too much. -c worked fine though. =)

    and thanks! this was very helpful.

  2. nathan

    oh, also applies for the section above the line i mentioned:

    eval `dircolors -b ~/dircolors.txt`; ls # C shell syntax

    should be

    eval `dircolors -c ~/dircolors.txt`; ls # C shell syntax

  3. Jamie Brown

    I preferred actually removing the colour, rather than changing it.

    Having a colour ls output is usually caused by having an alias for ls in some configuration file somewhere. If you can remove that alias, you can usually remove the colours.

    Alternatively you can put the following command into your .bashrc, .profile or whatever:

    unalias ls

    That should take ls back to its original state.

  4. carrel678

    Linux color management relies on the use of accurate International Color …. Mac OS X loads LUT adjustments automatically, while Linux and Microsoft not. In my laptop, I have tried this.

  5. Pingback: Changing Directory Listing (ls) in RHEL « Hai’s Blog

  6. silverhawk

    Awesome! The same issue had been troubling me for quite sometime, but never got a chance to do some research. Appreciate your effort in putting down this article online. THANKS!

  7. slw1jw

    Many thanks! This was something I have been meaning to do for quite a while. The write-up was easy to follow and very useful.

  8. Aravind

    is it possible to some how set the 8 bit colors. ?
    For example in the same promt, both in Bash and csh
    I can chnage the foreground/background colours of the prompt,

    echo -e ‘\e]11;#RRGGBB\a’ background

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )


Connecting to %s