LLI Wiki — My Digital Knowledge Base

Customizing ls colors with LS_COLORS

Change the LS_COLORS variable to alter ls colors in bash: SGR codes, file type keys and a terminal color preview.

The ls command colours entries according to the LS_COLORS environment variable. For one user, set it in ~/.bashrc. For all users, use the system-wide files: /etc/bash.bashrc (aliases and functions) or /etc/profile (environment variables). The complete list of startup files is in man bash.

Setting LS_COLORS

export LS_COLORS=$LS_COLORS:'di=0;35:'
export LS_COLORS="$LS_COLORS:ow=1;34:tw=1;34:"

Each entry has the form key=attributes;text;background. Entries appended at the end of the string override earlier ones for the same key.

Colour codes and attributes

Code Effect
0 Default
1 Bold
4 Underlined
5 Blinking (disabled on some terminals)
7 Reverse (foreground and background swapped)
8 Concealed

Text colours: 30 black, 31 red, 32 green, 33 orange, 34 blue, 35 purple, 36 cyan, 37 grey, 90 dark grey, 91 light red, 92 light green, 93 yellow, 94 light blue, 95 light purple, 96 turquoise, 97 white.

Background colours: 40 to 47 and 100 to 107, with the same colour names.

Example: di=1;4;31;42 shows directories in bold, underlined, red text on a green background.

File type keys

Key Type
bd (BLOCK, BLK) Block device
cd (CHAR, CHR) Character device
di (DIR) Directory
do (DOOR) Door
ex (EXEC) Executable file (has the x permission)
fi (FILE) Normal file
ln (SYMLINK, LINK, LNK) Symbolic link; target uses the colour of the file it points to
mi (MISSING) Non-existent file pointed to by a symbolic link
no (NORMAL, NORM) Normal text, global default
or (ORPHAN) Symbolic link pointing to a non-existent file
ow (OTHER_WRITABLE) Directory writable by others, without the sticky bit
pi (FIFO, PIPE) Named pipe (fifo)
sg (SETGID) File with the setgid bit
so (SOCK) Socket
st (STICKY) Directory with the sticky bit, not writable by others
su (SETUID) File with the setuid bit
tw (STICKY_OTHER_WRITABLE) Directory with the sticky bit and writable by others
*.extension Files with this extension, for example *.rpm

Previewing colours

Show the current value:

echo $LS_COLORS

Show the terminal colour combinations:

for i in 00{2..8} {0{3,4,9},10}{0..7}
do echo -e "$i \e[0;${i}mSubdermatoglyphic text\e[00m  \e[1;${i}mSubdermatoglyphic text\e[00m"
done

for i in 00{2..8} {0{3,4,9},10}{0..7}
do for j in 0 1
   do echo -e "$j;$i \e[$j;${i}mSubdermatoglyphic text\e[00m"
   done
done

Notes

Mastodon LLIMastodon SocialFosstodon