Adding Color to Bash List Command

One of the things I love is being able to enter into a directory via terminal, list the contents, and quickly see which ones are executable or not and which ones are directories or are not directories IN COLOR (not everyone is perfect right?).  This is specifically handy as I move through working with Slackware based distributions like the one I’m using now, Zenwalk Linux 6 and the /etc/rc.d/ directory.  Most of the Red Hat based distributions out there that I’ve used have this set automatically…so I’m used to it.  When I opened up a terminal in Zenwalk I found no color. A quick investigation of ~/.bashrc showed me that there are aliases set to display color…instead of ls I’d just use the letter L; 2 l’s for another option, 3 for another.  It’s interesting, but not normally how I do business.  See bashrc below:

devnet[~]$ cat ~/.bashrc
alias lll="ls -al --color"
alias ll="ls -l --color"
alias l='ls --color'<br />alias search='find . -name'
alias aterm="aterm -fg white -tr"
alias version='cat /etc/zenwalk-version'
alias dh='df -h'<br />export PS1='\u[\W]\$ '
export PAGER="most"<br />devnet[~]$

I don’t use those aliases much…others of you might do the ‘ll’ or ‘lll’ but it’s just not part of my routine.  So, I added in just after line 4 in the code above:

alias ls='ls --color'

You can do this a few different ways…with any command line editor like emacs, vi, or nano/pico. It’s really up to you. The easiest way for new users is to use a text editor:

devnet[~]$ mousepad ~/.bashrc

This will launch the mousepad text editor in XFCE. If you’re in Gnome, try gedit. If KDE, give kwrite a try. You should see something like the following window pop up for editing:

bashrcYou should see in the picture above, I’ve added in the alias needed to make me comfortable.  After that, File >> Save.  You’re set!  Except, you may notice that when you type the command ‘ls’ there are no colors.  So what gives?  Well, we haven’t loaded our ./bashrc since we changed it.  You don’t have to logout and back in to get things to work though…you can do this right from the terminal:

devnet[~]$ exec bash

Now try giving the ‘ls’ command a try again and you should see that it is in color.  I know a lot of the more advanced Linux guys out there are probably saying, “why’d he waste a post on this!  It’s easy” and you’d be right for a majority of users out there…I hope that this post finds some new user out there that may be stuck for an answer on how to do this.  One final note on this as well: You will have to do this as the root user and change /root/.bashrc in order for your root user to also have color.  Thanks for reading!

This content is published under the Attribution-Noncommercial-Share Alike 3.0 Unported license.

  • Pingback: Posts about Digg as of June 5, 2009 » The Daily Parr()

  • JamesD

    Thanks for the useful info. It’s so interesting

  • http://home.comcast.net/~masinick/ Brian Masinick

    I like your idea about using exec to replace the current shell; quite interesting and it works. I came from the old school where we learned to “dot” the files. Back then it was . ./.profile, then later, when we moved beyond the basic Bourne Shell, it was . ./.kshrc with the Korn Shell, then . ./.bashrc with the Bash shell, but “source” became a valid command – inherited from the C shell, csh, so you could also do source .bashrc. Most of the time you can also drop the relative path notion and say . .bashrc – which is close the the same number of characters to type as exec bash, but I think I can type exec bash faster, so I like it! (I used to exec back and forth between ksh, bash, tcsh, and zsh back in my UNIX system days, so I don’t know why I did not think about it long ago – clearly I WAS doing it! ;-)

    As far as the aliases regarding ls that I have, right now I am using these:

    alias l=’ls -CF’
    alias la=’ls -A’
    alias ll=’ls -l’
    alias ls=’ls –color=auto’

    If I were doing more frequent work with Bash (have not been using it nearly as much as the daily use I had in the past), I’d probably change the la to be alias la=’ls -al’ or maybe alias la=’ls -lart’ and I used to have an alias lsl=’ls -laR’ or alias lsl=’ls -al’.

    Thanks for the ideas and the discussion!

  • tricqster

    Thank you very much, although I know this, I read it again, because it was explained very nicely, and comprehensible even for beginners.
    I wish there would be more articles like this one, because very ofter manual pages, or sites that I find are difficult to understand (for me).
    Keep up this great work please.

  • http://www.jabz.info/contact/jonas-jared-jacek/ Jonas

    Thank you for that post. I was always looking for that in bash. I started using the z-shell for the colors (among other reasons) but never found out how to do that in bash. Thanks.

  • Pingback: Adding Color to Bash List Command Part II | Yet Another Linux Blog()

  • Evan

    I really appreciate these type of posts! Also, relating to Brian’s post: I have some experience with Unix and learned how to do this once but here’s the deal; I was learning from a UNIX book, on a RedHat 5 server and did my homework in Mac OS 10.4, Brian’s post about how all of the different ways he has done this was really cool. Anyways my point is that these types of simple posts are helpful to me even though I have done this before because Unix variants are a wild and confusing bunch that all have their own defaults and special commands.