Monday, February 26, 2007

Adding Color to "ls" in Bash

Ever since I installed Debian on our "ol' junker" Poweredge server at work, I've noticed that executing ls doesn't give me the same, colorful output as I used to get with the linux machines at work (and with every other linux install I've seen). I did some investigation, and found the following in in ~/.bashrc:

# ~/.bashrc: executed by bash(1) for non-login shells.
# see /usr/share/doc/bash/examples/startup-files (in the package bash-doc)
# for examples

# If running interactively, then:
if [ "$PS1" ]; then

# don't put duplicate lines in the history. See bash(1) for more options
# export HISTCONTROL=ignoredups

# enable color support of ls and also add handy aliases
eval `dircolors -b`
alias ls='ls --color=auto'
alias dir='ls --color=auto --format=vertical'
alias vdir='ls --color=auto --format=long'

The alias ls='ls --color=auto' should give me the colorful output I desire. What gives? Well, the comment at the top says that this file is executed when a non-login shell is opened. However, I want to see the colorful output even in my Putty terminal, not just in Xterm in Fluxbox or whatever. So, I cracked open ~/.bash_profile:

# ~/.bash_profile: executed by bash(1) for login shells.
# see /usr/share/doc/bash/examples/startup-files for examples.
# the files are located in the bash-doc package.
umask 022

# the rest of this file is commented out.
# include .bashrc if it exists

#if [ -f ~/.bashrc ]; then
# source ~/.bashrc

Aha! This is the script that is executed for a login shell. After uncommenting the last three lines and re-starting the shell, my ls's are looking very nice.

Update: To achieve this same thing for your root shell, you need only look to /root/.bashrc -- mine already had the color info commented out in that file, so I simply uncommented it.

No comments:

Disqus for A Nofsinger's Blog