ls colors

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'


Comments:

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.

Posted by none given on July 15, 2008 at 01:38 PM GMT #

who cares about colors!?

Posted by The Kernel. on July 15, 2008 at 04:07 PM GMT #

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.

Posted by Just Great on July 15, 2008 at 04:36 PM GMT #

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.

Posted by Rafael on July 16, 2008 at 01:29 AM GMT #

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.

Posted by Knut Grunwald on July 16, 2008 at 03:03 AM GMT #

[Trackback] Bookmarked your post over at Blog Bookmarker.com!

Posted by indecent on July 17, 2008 at 04:03 AM GMT #

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'

Posted by Luca Morettoni on August 20, 2008 at 02:52 AM GMT #

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

Posted by Brian Leonard on August 20, 2008 at 07:53 AM GMT #

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

Posted by Alvin on August 21, 2008 at 11:48 PM GMT #

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

Posted by matey on January 13, 2009 at 05:58 AM GMT #

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

Posted by Matey on January 13, 2009 at 06:07 AM GMT #

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

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

Posted by Ramshankar on August 20, 2009 at 04:02 AM GMT #

Hi It's my terminal http://denfromengl.files.wordpress.com/2010/03/terminal_color.png

and my bashrc http://omploader.org/vM3kzNQ/

Posted by den on March 28, 2010 at 05:15 AM GMT #

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

Posted by Kelsey on August 23, 2010 at 10:35 AM GMT #

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

Posted by ar on November 24, 2010 at 09:16 AM GMT #

Post a Comment:
  • HTML Syntax: NOT allowed
About

The Observatory is a blog for users of Oracle Solaris. Tune in here for tips, tricks and more as we explore the Solaris operating system from Oracle.

Connect with Oracle Solaris:


Search

Archives
« April 2014
SunMonTueWedThuFriSat
  
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
20
21
22
23
24
25
26
27
28
29
30
   
       
Today