Bash/Linux Tip: dircolors

in #linux6 years ago

Here's a quick tip on how to customize the colors that are used in the (Bash) shell when listing files.

I keep going nuts on the details in my posts, so here's my attempt at another short one. Or at least, it should be shorter, but then I'm writing this intro before I write the post, so we shall see.

One of the things that drew me to Linux was the ability to customize everything, and I do mean everything. One of the often overlooked details that you can customize are the colors used in the terminal when listing files with the ls command. You might wonder why you'd want to do that as the defaults are pretty sane, but I find that some of the colors are harder to see than others. Especially when the terminal background color has been modified.

Enabling Colors

If when you type ls you have color, you can skip this section.

The colors used when you type ls depend on a few different things, like environment variables or aliases. Generally your TERM variable needs to be set to something that ls recognizes as honoring escape sequences or an alias needs to be set for ls that actually invokes ls with an argument to display colors. So, depending on your terminal or Linux distribution you may or may not have these things set. If you're on Ubuntu, chances are your ls command is an alias, for example:

alias ls

On my system displays the following:

alias ls='ls --color=auto'

So, If you don't have color when typing ls on your system you can either enter the above (also put it in your .bashrc so it is always set) or you can manually type ls --color=auto or ls --color=always when you want to see colors.

To set the alias, either in your .bashrc file or just temporarily in the terminal, you type it in pretty much the same way:

alias ls='ls --color=always'

Customizing Colors

Colors in ls are controlled by the LS_COLORS environment variable. If you want to customize the colors used you will need to edit this variable. To help get you started, there's a command called dircolors that will print out the variable in a form that is suitable for appending to your .bashrc file. So I suggest first appending the output of dircolors to your .bashrc like so:

dircolors >> .bashrc

Note, the >> is important as it will append to rather than overwrite your .bashrc.

Once you have appended the colors to your .bashrc, edit the file in your favorite editor and start changing the values.

The format of the variable is a colon delimited set of name value pairs that are separated by equals signs. For each token the possible values are a number or multiple numbers separated by semi-colons.

The specific values for the different colors can be found in the dircolors man page, or online. Those charts are reproduced here for convenience.

ValueMeaning
0to restore default color
1for brighter colors
4for underlined text
5for flashing text
30for black foreground
31for red foreground
32for green foreground
33for yellow (or brown) foreground
34for blue foreground
35for purple foreground
36for cyan foreground
37for white (or gray) foreground
40for black background
41for red background
42for green background
43for yellow (or brown) background
44for blue background
45for purple background
46for cyan background
47for white (or gray) background

So, let's say I have a dark background and the blue color used by directories is too hard to read, well I can edit the LS_COLORS so that the entry for directories, which looks like this:

di=01;34

And let's say I want to make it a bold white, which is a 01 for brighter colors and a 37 for white foreground:

di=01;37

So the entire value would look like this:

LS_COLORS='rs=0:di=01;37: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:mi=00: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:*.arc=01;31:*.arj=01;31:*.taz=01;31:*.lha=01;31
:*.lz4=01;31:*.lzh=01;31:*.lzma=01;31:*.tlz=01;31:*.txz=01;31
:*.tzo=01;31:*.t7z=01;31:*.zip=01;31:*.z=01;31:*.Z=01;31:*.dz=01;31
:*.gz=01;31:*.lrz=01;31:*.lz=01;31:*.lzo=01;31:*.xz=01;31:*.zst=01;31
:*.tzst=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:*.alz=01;31:*.ace=01;31
:*.zoo=01;31:*.cpio=01;31:*.7z=01;31:*.rz=01;31:*.cab=01;31
:*.jpg=01;35:*.jpeg=01;35:*.mjpg=01;35:*.mjpeg=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:*.ogv=01;35:*.ogx=01;35:*.aac=00;36
:*.au=00;36:*.flac=00;36:*.m4a=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:*.oga=00;36:*.opus=00;36:*.spx=00;36:*.xspf=00;36:';

Note that the line has been broken up so it would fit on the page, there should be no carriage returns in the value.

If you want to test the color without editing the .bashrc file, you can set just the part you wish to change, for example:

export LS_COLORS="di=01;37"

And then colors will look as expected.

Without the variable:
default

With the variable:
modified

Other Color Customizations

As you may have guessed, you can make arbitrary colors for arbitrary file extensions. For example, to make .mkd files red (and keep the white directory colors):

export LS_COLORS="di=01;37:*.mkd=0;31"

And the result:
red

Summary

So, I covered the following points:

  • Colors with ls are based on the TERM variable and whether ls is invoked to support color.
  • Specific color values are controlled by the LS_COLORS shell variable
  • You can test them one at a time
  • To make color changes persist, save them in your .bashrc file

Also, this tip works in other Unix based operating systems, as well.

References


So, how did I do? Did I provide enough information without being overly long?

Also, If you have any questions, feel free to ask below.


Coin Marketplace

STEEM 0.26
TRX 0.13
JST 0.032
BTC 60837.81
ETH 2874.77
USDT 1.00
SBD 3.62