views:

1702

answers:

5

When you perform ls in a bash shell, sometimes there are colours to indicate different resource types, and you can enable/control this with the --color argument.

But neither the man page nor Google is providing an answer to the question:

What do these colours indicate by default, and how do I display what the current system uses?


UPDATE:

Thanks everyone for answers so far, however to make it easier to pick a winner, can anyone go a step further and provide a method to output descriptions in the colours they apply to.

Hmmm... my example doesn't work when posted (only when previewed), so if you preview this code it'll show what I mean...

<ul style="list-style:none; background:black; margin:0;padding:0.5em; width:10em">
  <li style="color:blue">directory</li>
  <li style="color:aqua">symbolic link</li>
  <li style="color:#A00000;">*.tar files</li>
  <li style="color:white">...</li>
</ul>

Thanks.

+3  A: 

You should be able to see the list of mappings in /etc/DIR_COLORS. You can override that by creating .dir_colors in your home directory.

chris
+6  A: 

The colors are defined by the $LS_COLORS environment variable. Depending on your distro, it is generated automatically when the shell starts, using ~/.dircolors or /etc/DIR_COLORS.

Edit:

To list color meanings, use this script:

eval $(echo "no:global default;fi:normal file;di:directory;ln:symbolic link;pi:named pipe;so:socket;do:door;bd:block device;cd:character device;or:orphan symlink;mi:missing file;su:set uid;sg:set gid;tw:sticky other writable;ow:other writable;st:sticky;ex:executable;"|sed -e 's/:/="/g; s/\;/"\n/g')
{
  IFS=:
  for i in $LS_COLORS
  do
    echo -e "\e[${i#*=}m$( x=${i%=*}; [ "${!x}" ] && echo "${!x}" || echo "$x" )\e[m"
  done
}
Juliano
I believe that it uses BOTH - defaults are set in DIR_COLORS, and anything in ~/.dir_colors takes precedence.
chris
Maybe... I don't know other distros, in Fedora, it is set in /etc/profile.d/colorls.sh, and it reads only one of them.
Juliano
Thats a nice script, but I'd also like to be able to see the colors for extensions you don't have listed.
Ian Kelling
@Ian: All extensions? This set is virtually infinite, it makes no sense. Files that don't have a specific color uses the "normal file" color, that is listed by the script.
Juliano
+2  A: 

Try "man 5 dir_colors" to see how it's set on your system. Mine doesn't have /etc/DIR_COLORS so it must be set somewhere else.

Paul Tomblin
A: 

Google for LS_COLORS for some useful links.

Edit: To list the colors, this simple bash script may give an idea:

IFS=:
set $LS_COLORS
for C in $*
do
    IFS='='
    set $C
    echo -e "\033[$2m$1\033[00m"
done
anon
Thanks Neil... unfortunately there isn't a way to accept a secondary/partial answer, and Juliano's one is slightly more complete.
Peter Boughton
+4  A: 

Running the command "dircolors -p" will print all your current settings for the colouring.

http://linux.about.com/library/cmd/blcmdl1_dircolors.htm

Mark Davidson
"dircolors -p" prints defaults, not the current settings
Juliano