Linux Terminal Colors

1. Enabling colored output

1.1 ls –color

In Linux, you use the ls command to display the contents of a folder. ls can be used with the –color=WHEN

attribute to display the contents colored according to the LS_COLOR environment variable:

ls --color={never|always|auto}
#auto - uses colors only when stdout is connected to the terminal

You usually configure an alias to force ls to use the color option. When using bash as the default shell, you can configure the alias either globally in /etc/bash.bashrc or for each user in ~/.bashrc:

alias ls="ls --color=auto"

1.2 LS_COLORS

You can see the current value of LS_COLORS with the echo command, but the output will not be very friendly:

echo $LS_COLORS
no=00:fi=00:di=01;34:ln=01;36:pi=40;33:so=01;35:bd=40;33;01:cd=40;33;01:ow=40;32:
ex=01;32:*.cmd=01;32:*.exe=01;32:*.com=01;32:*.btm=01;32:*.bat=01;32:*.tar=01;31:
*.tgz=01;31:*.arj=01;31:*.taz=01;31:*.lzh=01;31:*.zip=01;31:*.z=01;31:*.Z=01;31:
*.gz=01;31:*.jpg=01;35:*.gif=01;35:*.bmp=01;35:*.xbm=01;35:*.xpm=01;35:*.tif=01;35:

The format of the variable is FILETYPE1=COLOUR1:FILETYPE2=COLOUR2:FILETYPE3=COLOUR3:… where FILETYPE represents either a type of file (regular file, link, folder,…) or a regex match of the filename -usually to match extensions: (*.cmd,*.gz,*.jpg,…)
A lot of info in this article is taken from http://www.bigsoft.co.uk/blog/index.php/2008/04/11/configuring-ls_colors and so is the following table describing the FILETYPEs:

LS_COLORS DIR_COLORS Description
no NORMAL, NORM Global default, although everything should be something
fi FILE Normal file
di DIR Directory
ln SYMLINK, LINK, LNK Symbolic link. If you set this to ‘target’ instead of a numerical value, the color is as for the file pointed to.
pi FIFO, PIPE Named pipe
do DOOR Door
bd BLOCK, BLK Block device
cd CHAR, CHR Character device
or ORPHAN Symbolic link pointing to a non-existent file
so SOCK Socket
su SETUID File that is setuid (u+s)
sg SETGID File that is setgid (g+s)
tw STICKY_OTHER_WRITABLE Directory that is sticky and other-writable (+t,o+w)
ow OTHER_WRITABLE Directory that is other-writable (o+w) and not sticky
st STICKY Directory with the sticky bit set (+t) and not other-writable
ex EXEC Executable file (i.e. has ‘x’ set in permissions)
mi MISSING Non-existent file pointed to by a symbolic link (visible when you type ls -l)
lc LEFTCODE, LEFT Opening terminal code
rc RIGHTCODE, RIGHT Closing terminal code
ec ENDCODE, END Non-filename text
*.extension   Every file using this extension e.g. *.jpg

The COLOURS that can be used are written as combinations of the following attributes, separated by “;”:

Effect Foreground color Background color
00 – none
01 – bold
04 – underline
05 – blink
07 – reversed
08 – concealed
30 – black
31 – red
32 – green
33 – yellow
34 – blue
35 – magenta
36 – cyan
37 – light gray
90 – dark gray
91 – light red
92 – light green
93 – light yellow
94 – light blue
95 – light purple
96 – turquoise
97 – white
40 – black
41 – red
42 – green
43 – yellow
44 – blue
45 – magenta
46 – cyan
47 – light gray
100 – dark gray
101 – light red
102 – light green
103 – light yellow
104 – light blue
105 – light purple
96 – turquoise

So 01;31;43 would translate to a red color over yellow background and bold.
Now you can set LS_COLORS in /etc/bash.bashrc or ~/.basrc to match your needs.

1.3 DIR_COLORS

The current values used for LS_COLORS, including defaults can be seen using:

dircolors -p|less

This command outputs all settings regarding to LS_COLOR, including default values. Custom values that override the defaults can be set in the /etc/DIR_COLORS file, but can also be further customized in the following files:/etc/DIR_COLORS.$TERM, ~/.dircolors, ~/.dircolors.$TERM, ~/.dir_colors, ~/.dir_colors.$TERM
So what are the settings that can be customized? First, there is the type of terminal where LS_COLORS are used. Terminals not in this list will not use the color option.

TERM xterm
TERM rxvt-unicode

Then, there is the list of file types that will be colored:

# Below are the color init strings for the basic file types. A color init
# string consists of one or more of the following numeric codes:
# Attribute codes:
# 00=none 01=bold 04=underscore 05=blink 07=reverse 08=concealed
# Text color codes:
# 30=black 31=red 32=green 33=yellow 34=blue 35=magenta 36=cyan 37=white
# Background color codes:
# 40=black 41=red 42=green 43=yellow 44=blue 45=magenta 46=cyan 47=white
#NORMAL 00 # no color code at all
#FILE 00 # regular file: use no color at all
RESET 0 # reset to "normal" color
DIR 01;34 # directory
LINK 01;36 # symbolic link. (If you set this to 'target' instead of a
 # numerical value, the color is as for the file pointed to.)
MULTIHARDLINK 00 # regular file with more than one link
FIFO 40;33 # pipe
SOCK 01;35 # socket
DOOR 01;35 # door
BLK 40;33;01 # block device driver
CHR 40;33;01 # character device driver
ORPHAN 40;31;01 # symlink to nonexistent file, or non-stat'able file
SETUID 37;41 # file that is setuid (u+s)
SETGID 30;43 # file that is setgid (g+s)
CAPABILITY 30;41 # file with capability
STICKY_OTHER_WRITABLE 30;42 # dir that is sticky and other-writable (+t,o+w)
OTHER_WRITABLE 34;42 # dir that is other-writable (o+w) and not sticky
STICKY 37;44 # dir with the sticky bit set (+t) and not other-writable
# This is for files with execute permission:
EXEC 01;32

And in the end there is a list of extensions that can be coloured:

# List any file extensions like '.gz' or '.tar' that you would like ls
# to colorize below. Put the extension, a space, and the color init string.
# (and any comments you want to add after a '#')
# If you use DOS-style suffixes, you may want to uncomment the following:
#.cmd 01;32 # executables (bright green)
#.exe 01;32
#.com 01;32
#.btm 01;32
#.bat 01;32
# Or if you want to colorize scripts even if they do not have the
# executable bit actually set.
#.sh 01;32
#.csh 01;32
 # archives or compressed (bright red)
.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

2. Customize Color Scheme

2.1 Modifying ~/.Xresources

You can further modify the color scheme by instructing your terminal application to use other colors instead of the defaults black, red, green and so on. Terminal applications such as xterm, aterm, rxvt or urxvt use the ~/.Xresources file to store user configurations.
In this file you can override the default colors with:

*background: #282828
*foreground: #d9d9d9
! Instead of black and bright_black
*color0: #000000
*color8: #282828
! Instead of red and bright_red
*color1: #b26179
*color9: #d09dac
! Instead of green and bright_green
*color2: #79b261
*color10: #acd09d
! Instead of yellow and bright_yellow
*color3: #b29a61
*color11: #d0c09d
! Instead of blue and bright_blue
*color4: #6179b2
*color12: #9dacd0
! Instead of magenta and bright_magenta
*color5: #9a61b2
*color13: #c09dd0
! Instead of cyan and bright_cyan
*color6: #61b29a
*color14: #9dd0c0
! Instead of white and bright_white
*color7: #d9d9d9
*color15: #ffffff

A great tool for creating color schemes for your terminal application is http://ciembor.github.io/4bit. It can export the configuration needed for most terminal applications.

2.2 Loading ~/.Xresources

In the end, you will need to load the ~/.Xresources file every time your X session starts. To do this, add this config to your ~/.xinitrc file:

[[ -f ~/.Xresources ]] && xrdb -merge ~/.Xresources

It will check if the .Xresources file exists and will merge this data into the X server config database.

Leave a Reply

Your email address will not be published. Required fields are marked *