Americas

  • United States
sandra_henrystocker
Unix Dweeb

Making your command line more helpful and fun!

How-To
Apr 21, 20164 mins
Data CenterLinux

The default terminal settings on most linux systems is to use colors to serve as hints about file types and permissions. For example, you’ll see directory names in blue, executables in green, and file archives in red. And, if you don’t want to see the colors — like when they’re just too distracting, you can turn them off easily. The command line colors are set when the ls command is aliased to an ls command that uses the –color option like this:

alias ls='ls --color=auto'

To turn off font colors for some period of time, you can just turn off the alias.

$ unalias ls

To turn off font colors all of the time, you can put the unalias command in your ~/.bashrc file or remove the alias command that sets it up in the first place, though that might be in your ~/.bashrc file or some system file.

You can also make individual changes to your font color palette. Say you don’t like seeing blue as the font color that displays the names of directories. Maybe your background color is a close blue. You can change the color used for just directories with a command like this:

LS_COLORS=$LS_COLORS:'di=35:' ; export LS_COLORS

So, where did that come from? The di in that command signifies directories. It and other particular file types that you can specify are listed here:

di = directory
fi = file
ln = symbolic link
pi = fifo file
so = socket
bd = block (buffered) special file
cd = character (unbuffered) special file
or = symbolic link pointing to a nonexistent file
mi = non-existent file pointed that a symbolic link
     points out (showing up in the long listing)
ex = an executable file
*.rpm = files with the ending .rpm

The 35 in the command indicates the color — 35 means purple. Some of the options in the list below can be combined. For example, you could use a purple font with a green background if you use di=35;42.

Here is a list of your options, though I have to admit to some of these color combinations combined with flashing would probably make me dizzy.

0  = default colour
1  = bold
4  = underlined
5  = flashing text
7  = reverse field
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
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

Another cute trick that I picked up online is making the color of your prompt change depending on whether the command you just entered completed successfully or not. This trick uses some of what we’ve seen above (e.g., selecting the right font colors), but adds a function to your ~/.bashrc file that formats your prompt after evaluating the exit status of your last command.

Add a function like this to your ~/.bashrc file and your prompt will go from green (normal) to red (something is wrong) when you type a command that fails for some reason.

function exstat {
    EXSTAT="$?"
    RED="[
sandra_henrystocker
Unix Dweeb

Sandra Henry-Stocker has been administering Unix systems for more than 30 years. She describes herself as "USL" (Unix as a second language) but remembers enough English to write books and buy groceries. She lives in the mountains in Virginia where, when not working with or writing about Unix, she's chasing the bears away from her bird feeders.

The opinions expressed in this blog are those of Sandra Henry-Stocker and do not necessarily represent those of IDG Communications, Inc., its parent, subsidiary or affiliated companies.