X

News, tips, partners, and perspectives for the Oracle Solaris operating system

ls colors

Guest Author

One of the goals of the OpenSolaris distribution is to provide an environment that looks at least somewhat familiar to users of other operating systems, and especially to Linux users who are familiar with the GNU tools.  For example, the default shell for a new user is bash.  And the default content of the .bashrc file in a new user's home directory is:

#
# Simple profile places /usr/gnu/bin at front,
# adds /usr/X11/bin, /usr/sbin and /sbin to the end.
#
# Use less(1) as the default pager for the man(1) command.
#
export PATH=/usr/gnu/bin:/usr/bin:/usr/X11/bin:/usr/sbin:/sbin
export MANPATH=/usr/gnu/share/man:/usr/share/man:/usr/X11/share/man
export PAGER="/usr/bin/less -ins"
#
# Define default prompt to <username>@<hostname>:<path><"($|#) ">
# and print '#' for user "root" and '$' for normal users.
#
PS1='${LOGNAME}@$(hostname):$(
    [[ "$LOGNAME" = "root" ]] && printf "${PWD/${HOME}/~}# " ||
    printf "${PWD/${HOME}/~}\\$ ")'

Note that the GNU command line tools are first on the PATH. 

So for someone like me, who has spent quite a bit of time using Ubuntu, this works out great.  But by default, there is something missing: where is the colorful output that I am used to seeing from the ls command? 

If you look at the default .bashrc file from an Ubuntu 8.04 installation, you will see that an alias gets defined to specify the --color option:

#
# enable color support of ls and also add handy aliases
if [ "$TERM" != "dumb" ] && [ -x /usr/bin/dircolors ]; then
    eval "`dircolors -b`"
    alias ls='/usr/gnu/bin/ls --color=auto'
    #alias dir='ls --color=auto --format=vertical'
    #alias vdir='ls --color=auto --format=long'

  #alias grep='grep --color=auto'
    #alias fgrep='fgrep --color=auto'
    #alias egrep='egrep --color=auto'
fi

I can add that logic to my .bashrc file on OpenSolaris 2008.05 and then get exactly the same color output from ls as I get on Ubuntu, right?  Well, almost.  It turns out that the pre-defined database of colors included in /usr/bin/dircolors on Ubuntu 8.04 is slightly different - there are a few additional terminal and file types defined.  For example, there is an entry for .lzma, which I suppose might be handy if I create files with 7za and decide to use .lzma for the extension.

So I copied the color definitions from my Ubuntu installation over to a file called gregg-dircolors-db.txt in my home directory on OpenSolaris.  I even added a few more entries to customize it a bit - you can download it here.  The final step was to add this to the end of my .bashrc file on OpenSolaris:

# enable color support of ls and also add handy aliases 
if [ "$TERM" != "dumb" ] && [ -x /usr/bin/dircolors ]; then
eval "`dircolors -b ./gregg-dircolors-db.txt`"
alias ls='ls --color=auto'
fi

And now when I use the ls command, I see colors in the output:

Update - some other options you may find useful (thanks to the comments on this post):

To have a indicator (directory, executable, link) appended to the entries, add the -F option.

    alias ls='/usr/gnu/bin/ls --color=auto -F'

And to list the directories first, add the --group-directories-first option:

   alias ls='/usr/gnu/bin/ls --color=auto -F --group-directories-first'


Join the discussion

Comments ( 15 )
  • none given Tuesday, July 15, 2008

    Sure, ls -V breaks and you lose the ability to see or modify ACL's... but you have colours...

    "linux familiarity" is getting out of hand, this is ridiculous.


  • The Kernel. Tuesday, July 15, 2008

    who cares about colors!?


  • Just Great Tuesday, July 15, 2008

    Yep, that great linuxy feel is wonderful. Totally alien to those of us who've used Sun gear and Solaris for the past 15 years, but who cares about those whiners?

    Stop the madness. Enough of this look & linux feel crap.


  • Rafael Wednesday, July 16, 2008

    Ah, yes.. Can't bring up colors in terminal output without getting some sort of flame :)

    IMO, the more information, the better.

    Keep up the good work fellas.


  • Knut Grunwald Wednesday, July 16, 2008

    The tip is ok, but you don't need to PATH the whole GNU-Land for this, since aliases allow for absolute paths. The biggest problem in colored ls is the color choice, which is ... ehm indecent.

    I hope it is not as bad, as with cygwin, where you can't see some files, since they have the same color as your terminal background.

    Since i don't use light text, on dark background, this is inevitable with the auto-colorscheme.

    Enough bashing. I like the blog entry, since it makes solaris more useable for people with linux background.


  • indecent Thursday, July 17, 2008
    [Trackback] Bookmarked your post over at Blog Bookmarker.com!
  • Luca Morettoni Wednesday, August 20, 2008

    I think that the -F flag is more usefull than colors, it append indicator (one of \*/=>@|) to entries

    Simply add to yours .bashrc:

    alias ls='ls -F'


  • Brian Leonard Wednesday, August 20, 2008

    Totally agree - thanks for the tip Luca. Now I know what the colors stand for :-).


  • Alvin Thursday, August 21, 2008

    Luca!! Thx man, that was all I needed.


  • matey Tuesday, January 13, 2009

    Thanks a lot, I could not figure out how to put that stupid auto/always after the color switch.

    The man files really need to get up speed they just put a lot of those dumb sqr brackets and confuse the hell out of you.

    like it says in ls --help

    -C (for col. unrelated) and then --color [=WHEN]

    what the hell is thatsupposed to mean?

    and it is important to have the colors, how else would you know if you are dealing with a directory or a file and what kind of a file? is it executable or whatever?

    I know you CAN ALWAYS DO ALL THOSE SWITCHES BUT WHEN YOU CAN PUT A SPOON STRAIGHT FROM THE PLATE IN YOUR MOUTH WHY WRAP YOUR ARM AROUND YOUR NECK FIRST ?

    THANKS (OOPS CAPS LOCK WENT ON, I cant stand too much typing, that is the secretary's job not mine!

    ;-p


  • Matey Tuesday, January 13, 2009

    Oh BTW, I put this PS1 in my profile and I get this colorful prompt;

    PS1='[\\033[01;32m\\]\\u\\033[01;33m\\]@\\[\\033[01;00m\\]\\h:\\[\\033[01;31m\\]\\W\\[\\033[00m\\]>'

    but I have 2 extra spaces which I do not know how to get rid of?

    Would someone please tell me where these spaces are? I tried many things already to no avail they just messed up my prompt; here's what it looks like (B&W) (here)

    [root@Ubuntu8:~>

    and of course those escape chars can be seen here but not on my screen so it looks like this to me:

    [ root @Ubuntu8:~>

    As you can see there are 2 extra spaces, 1 b4 root and 1 after?

    THANKS A LOT!

    btw what is 6 + 85 = ?? gee I am stuck now ;-)

    the question below this box;

    LOL J/K!

    :D


  • Ramshankar Thursday, August 20, 2009

    Really useful and saved me time searching for info on doing this.

    Also useful is "ls --group-directories-first" option.


  • Kelsey Monday, August 23, 2010

    /usr/bin/ls supports the --color option as well. This tip still applies if you replace the GNU ls with the solaris provided /usr/bin/ls.

    There are comments above lamenting that GNU ls will break the -V option and doesn't support ACLs. If you want colors and ACL support, use /usr/bin/ls instead of /usr/gnu/bin/ls.

    /usr/bin/ls does not support the --group-directories-first option, however.


  • ar Wednesday, November 24, 2010

    the trouble with "unix gurus" is they are totally unhelpful to put it mildly. it is not that the rest of us are dumb. well some are...

    however this is a great post that helps us who can't see too well adjust their terminal to be able to see clearer. Thank you Gregg and for those of you who want to argue .... BRING IT ON


  • Tye Z Friday, October 13, 2017
    Good tip! I didn't even know about the /usr/gnu directory, so that was a big win! :) Makes SunOS WAY more usable.
Please enter your name.Please provide a valid email address.Please enter a comment.CAPTCHA challenge response provided was incorrect. Please try again.Captcha