Blog-Archiv

Montag, 6. Juni 2016

LINUX Terminal ls colors

I really like the LINUX operating system, but some things are quite hard to understand. For example, when I opened a terminal-screen, changed to the WINDOWS disk of my system, did a ls -la there to see files and directories, I saw this:

Not really good readable, the green directories, right? On a bright screen it looks even worse, not readable at all.
I was seeing things like this for decades, so now it's time.

When I searched for help on the web, I found useful pages, but usefulness ends when the things you have to do get so complicated that you need lots of time for it. I found myself confronted with following environment-variable, generated by a tool called dircolors in /usr/bin/ (following is the output of that tool).

LS_COLORS='rs=0:di=01;34:ln=01;36:mh=00:pi=40;33:so=01;35:do=01;35:bd=40;33;01:cd=40;33;01:or=40;31;01:su=37;41:sg=30;43:ca=30;41:tw=30;42:ow=34;42:st=37;44:ex=01;32:*.tar=01;31:*.tgz=01;31:*.arj=01;31:*.taz=01;31:*.lzh=01;31:*.lzma=01;31:*.tlz=01;31:*.txz=01;31:*.zip=01;31:*.z=01;31:*.Z=01;31:*.dz=01;31:*.gz=01;31:*.lz=01;31:*.xz=01;31:*.bz2=01;31:*.bz=01;31:*.tbz=01;31:*.tbz2=01;31:*.tz=01;31:*.deb=01;31:*.rpm=01;31:*.jar=01;31:*.war=01;31:*.ear=01;31:*.sar=01;31:*.rar=01;31:*.ace=01;31:*.zoo=01;31:*.cpio=01;31:*.7z=01;31:*.rz=01;31:*.jpg=01;35:*.jpeg=01;35:*.gif=01;35:*.bmp=01;35:*.pbm=01;35:*.pgm=01;35:*.ppm=01;35:*.tga=01;35:*.xbm=01;35:*.xpm=01;35:*.tif=01;35:*.tiff=01;35:*.png=01;35:*.svg=01;35:*.svgz=01;35:*.mng=01;35:*.pcx=01;35:*.mov=01;35:*.mpg=01;35:*.mpeg=01;35:*.m2v=01;35:*.mkv=01;35:*.webm=01;35:*.ogm=01;35:*.mp4=01;35:*.m4v=01;35:*.mp4v=01;35:*.vob=01;35:*.qt=01;35:*.nuv=01;35:*.wmv=01;35:*.asf=01;35:*.rm=01;35:*.rmvb=01;35:*.flc=01;35:*.avi=01;35:*.fli=01;35:*.flv=01;35:*.gl=01;35:*.dl=01;35:*.xcf=01;35:*.xwd=01;35:*.yuv=01;35:*.cgm=01;35:*.emf=01;35:*.axv=01;35:*.anx=01;35:*.ogv=01;35:*.ogx=01;35:*.aac=00;36:*.au=00;36:*.flac=00;36:*.mid=00;36:*.midi=00;36:*.mka=00;36:*.mp3=00;36:*.mpc=00;36:*.ogg=00;36:*.ra=00;36:*.wav=00;36:*.axa=00;36:*.oga=00;36:*.spx=00;36:*.xspf=00;36:';
export LS_COLORS

This string is composed of colon-separated entries (":"), each describing the text-attributes and colors for a specfic file-system node. The type of the node is given by the first identifier, e.g. "di" stands for "directory" (it's time to get rid of that abbreviation fashion!). Here are some of them (how often has this been copied now, and still some types missing?):


bd = block (buffered) device, special file
cd = character (unbuffered) device, special file
do = door
di = directory
ec = ENDCODE, non-filename text
ex = file which is executable (ie. has 'x' set in permissions).
fi = file
lc = LEFTCODE, opening terminal code
ln = symbolic link
mi = non-existent file pointed to by a symbolic link (visible when you type ls -l)
no = normal, global default
or = orphan, symbolic link pointing to a non-existent file (orphan)
pi = fifo file, named pipe
rc = RIGHTCODE, closing terminal code
sg = file that is setgid (g+s)
so = socket file
st = sticky bit set (+t) and not other-writable directory
su = file that is setuid (u+s)
tw = sticky and other-writable (+t,o+w) directory
ow = other-writable (o+w) and not sticky directory
*.extension = every file using this extension e.g. *.jpg

To the right of the equals-sign ("=") there are 0-n text-attributes, then foreground-, then background-color, all semicolon-separated (";"), terminated again by colon (":") (shouldn't colon be the field-separator?). Here are their number-codes (how often has this been copied now?):


0   = default colour
1   = bold
4   = underlined
5   = flashing text
7   = reverse field
8   = concealed
30  = black
31  = red
32  = green
33  = orange
34  = blue
35  = purple
36  = cyan
37  = grey
40  = black background
41  = red background
42  = green background
43  = orange background
44  = blue background
45  = purple background
46  = cyan background
47  = grey background
90  = dark grey
91  = light red
92  = light green
93  = yellow
94  = light blue
95  = light purple
96  = turquoise
97  = white
100 = dark grey background
101 = light red background
102 = light green background
103 = yellow background
104 = light blue background
105 = light purple background
106 = turquoise background

But what node-type is a WINDOWS directory regarded to be? As you can see, the type is not documented (or maybe the documentation has been lost :-). So I had to guess the color and look for it in that long-long string. After all, I managed to find out by try & error that the nasty green background-color sits on

tw=30;42:ow=34;42:

which I decode as

for node-type "tw", use color 30, background 42
for node-type "ow", use color 34, background 42

So, now, how to fix that? In my $HOME/.bashrc I found this:

if [ -x /usr/bin/dircolors ]; then
    test -r ~/.dircolors && eval "$(dircolors -b ~/.dircolors)" || eval "$(dircolors -b)"
    # ....
fi

and added this line: LS_COLORS="${LS_COLORS}:tw=30:ow=34:"

if [ -x /usr/bin/dircolors ]; then
    test -r ~/.dircolors && eval "$(dircolors -b ~/.dircolors)" || eval "$(dircolors -b)"
    LS_COLORS="${LS_COLORS}:tw=30:ow=34:"
    # ....
fi

That means, I simply removed the background-color.

Hm, still not well visible.
To completely get rid of these colors, simply comment out the dircolors section in $HOME/.bashrc.


After all, here is the list of things I had to do for that peanut:

  1. find out that dircolors is responsible for these colors
  2. learn dircolors entry syntax
  3. learn type codes, learn color codes, to find out where the problem sits in my LS_COLORS environment variable
  4. in that long-long string, search for codes I suspected to be the problems
  5. find out that you can override entries by repeating them at end of LS_COLORS
  6. find out how dircolors was integrated
  7. edit .bashrc, test and verify that changes help
  8. write this Blog, because on my next full LINUX upgrade I may find the same problem again, and this is nothing you will keep in mind

But I won't send a bill for this, because LINUX is a free operating system :-!

Essentially I just want to point out how important it is in software-development to

  • keep it simple (use some standard configuration syntax)
  • use long names (for types, text-attributes and colors, do not encode everything)
  • keep it small (seven types would have been enough)
  • take yourself time to find good names (should have been filecolors, not dircolors)
  • make sure that enough documentation is available on the web (I tried man dircolors, I tried info coreutils 'dircolors invocation', I tried dircolors -p, there is no reference online).



Keine Kommentare: