Bash - Changing the colours

From Da Nerd Mage Wiki
Revision as of 18:38, 20 January 2022 by Tinker (talk | contribs)
Jump to navigation Jump to search

Modern Linux & UNIX systems can colourise the output of ls.

Sadly, the default colour choices can make your eyes bleed...

Fortunately, you can customise the colour choices.

To see the default (or, at least, current) colours your system shows for everything, paste the following code block directly at the command line.

LS COLORS Sampler.png
( # Run in a subshell so it won't crash current color settings dircolors -b >/dev/null
IFS=:
for ls_color in ${LS_COLORS[@]}; do # For all colors
color=${ls_color##*=}
ext=${ls_color%%=*}
echo -en "\E[${color}m${ext}\E[0m " # echo color and extension
done
echo
)

LS_COLORS

You control the colours with an environment variable named LS_COLORS which you can set in your .bashrc file. (On many Linux variants, you can simply define the variable in a file named .dircolors, but not all. So putting it in your .bashrc file is the safest bet.)

What the codes mean

Key Defaults Name Notes
  color:background:effect    
no   NORMAL, NORM Global default, although everything should be something
fi   FILE Normal file
di 01;34 DIR Directory
ln 01;36 SYMLINK, LINK, LNK Symbolic link. If you set this to 'target' instead of a numerical value, the colour is as for the file pointed to.
pi 40;33 FIFO, PIPE Named pipe
do 01;35 DOOR Door
bd 40;33;01 BLOCK, BLK Block device
cd 40;33;01 CHAR, CHR Character device
or 40;31;01 ORPHAN Symbolic link pointing to a non-existent file
so 01;35 SOCK Socket
su 37;41 SETUID File that is setuid (u+s)
sg 30;43 SETGID File that is setgid (g+s)
tw 30;42 STICKY_OTHER_WRITABLE Directory that is sticky and other-writable (+t,o+w)
ow 34;42 OTHER_WRITABLE Directories that are writeable by other users
st 37;44 STICKY Directory with the sticky bit set (+t) and not other-writable
ex 01;32 EXEC Executable file (i.e. has 'x' set in permissions)
mi 00 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 various   Every file using this extension e.g. *.jpg
rs 0    
mh 00 MULTIHARDLINK  
ca 30;41 CAPABILITY  

Colour & Effect options

Foreground Background Effects
Code Property Code Property Code Property
30 Black 40 Black 00 Default
31 Red 41 Red 01 Bold
32 Green 42 Green 04 Underlined
33 Orange 43 Orange 05 Flashing Text
34 Blue 44 Blue 07 Reversed
35 Purple 45 Purple 08 Concealed
36 Cyan 46 Cyan    
37 Grey 47 Grey    
Extra Colours
90 Dark Grey 100 Dark Grey    
91 Light Red 101 Light Red    
92 Light Green 102 Light Green    
93 Yellow 103 Yellow    
94 Light Blue 104 Light Blue    
95 Light Purple 105 Light Purple    
96 Turquoise 106 Turquoise    
97 White 107 White