X
Business

Customizing colors with the ls utility

Learn to set up and use the ls utility in Linux. This utility allows you to display different colors on your terminal.
Written by Vincent Danen, Contributor

Most Linux distributions come with the ls utility configured to display directories and files in color.

This is allowed because most terminals support ANSI escape sequences which, among other things, allow for the display of different colors. Depending on your preferred terminal settings, the defaults may not be good enough; if you use a black background for the terminal, the directory names may be too dark to read properly; if you use a lighter colored background, the color for image files may be too light.

You can easily customize the colors of your shell by modifying a few files. The program used to define what colors are associated with what files is the dircolors tool, which uses a default built-in database. You can start out by creating your own file to customize by using:

$ dircolors --print-database >~/.dircolors

Once you have the database in a text file, you need to tell dircolors to use that file rather than the internal database. This can be done by editing a shell startup file such as ~/.bashrc or ~/.zshrc to look like:

# set dircolors

eval $(dircolors -b ~/.dircolors)

If you use a csh-style shell, you would use the following instead:

eval `dircolors -c ~/.dircolors`

Chances are ls is already set as an alias to support colors, but if it isn't, you can easily do set it in a shell startup file like this:

alias ls="ls -F --color=auto"

This tells ls to only output the ANSI escape sequences if the standard output is a terminal, otherwise it doesn't print the ANSI escape sequences—this is good if you have shell scripts running out of cron that turn into e-mails that don't parse those escape sequences.

Vincent Danen has been using Linux for nearly two years and obtained his Linux Administrator certification from Tekmetrics.com.

Editorial standards