Tuesday, April 1, 2008

Configuring color scheme for the 'ls' command

I have been having a hard time with the blue color in a Cygwin terminal (mintty) when I 'ssh' to other servers. It turns out I could fix this problem on a Linux server pretty easily by running this command:
$ dircolors -p > ~/.dircolors
I can also edit this file, '.dircolors', to customize my color scheme. Remember to re-login to see the changes.

As I research a little deeper, I found out how this file is being loaded. I see this code in "/etc/bashrc":
    for i in /etc/profile.d/*.sh; do
        if [ -r "$i" ]; then
            if [ "$PS1" ]; then
                . $i
            else
                . $i >/dev/null 2>&1
            fi
        fi
    done
And under "/etc/profile.d/", I see a file called, colorls.sh. In it I see the following code:
# color-ls initialization

alias ll='ls -l' 2>/dev/null
alias l.='ls -d .*' 2>/dev/null

COLORS=/etc/DIR_COLORS
[ -e "/etc/DIR_COLORS.$TERM" ] && COLORS="/etc/DIR_COLORS.$TERM"
[ -e "$HOME/.dircolors" ] && COLORS="$HOME/.dircolors"
[ -e "$HOME/.dir_colors" ] && COLORS="$HOME/.dir_colors"
[ -e "$HOME/.dircolors.$TERM" ] && COLORS="$HOME/.dircolors.$TERM"
[ -e "$HOME/.dir_colors.$TERM" ] && COLORS="$HOME/.dir_colors.$TERM"
[ -e "$COLORS" ] || return

eval `dircolors --sh "$COLORS" 2>/dev/null`
[ -z "$LS_COLORS" ] && return

if ! egrep -qi "^COLOR.*none" $COLORS >/dev/null 2>/dev/null ; then
        alias ll='ls -l --color=tty' 2>/dev/null
        alias l.='ls -d .* --color=tty' 2>/dev/null
        alias ls='ls --color=tty' 2>/dev/null
fi
So, the 'ls' color scheme file could be .dircolors, .dir_colors, or dircolors.xterm etc.

No comments:

Post a Comment