I understand ls uses dircolors to display colored output. dircolors has default database of colors associated with file extensions, which can be printed wiht the command

dircolors --print-database

From man dir_colors I read, the system-wide database should be located in /etc/DIR_COLORS. But this file does not exist on my system (Debian). How can I modify system-wide color settings for dircolors? Where does the command dircolors --print-database take the settings from, when no file exists.

I am aware that user can have user-specific file ~/.dircolors with his settings, but this is not suitable for me, since I need to change the settings for everybody.

A second questions is, whether it is possible to use 8-bit colors for dircolors. My terminal is xterm-256color.

up vote 31 down vote accepted

ls takes it color settings from the environment variable LS_COLORS. dircolors is merely a convenient way to generate this environment variable. To have this environment variable take effect system-wide, put it in your shell's startup file.

For bash, you'd put this in /etc/profile:

# `dircolors` prints out `LS_COLORS='...'; export LS_COLORS`, so eval'ing
# $(dircolors) effectively sets the LS_COLORS environment variable.

eval "$(dircolors /etc/DIR_COLORS)"

For zsh, you'd either put it in /etc/zshrc or arrange for zsh to read /etc/profile on startup. Your distribution might have zsh do that already. I just bring this up to point out that setting dircolors for truly everybody depends on the shell they use.

As for where dircolors gets its settings from, when you don't specify a file it just uses some builtin defaults.

You can use xterm's 256 color escape codes in your dircolors file, but be aware that they'll only work for xterm compatible terminals. They won't work on the Linux text console, for example.

The format for 256 color escape codes is 38;5;colorN for foreground colors and 48;5;colorN for background colors. So for example:

.mp3  38;5;160                   # Set fg color to color 160      
.flac 48;5;240                   # Set bg color to color 240
.ogg  38;5;160;48;5;240          # Set fg color 160 *and* bg color 240.
.wav  01;04;05;38;5;160;48;5;240 # Pure madness: make bold (01), underlined (04), blink (05), fg color 160, and bg color 240!

Where does the command dircolors --print-database take the settings from, when no file exists.

As per the manual, it uses a precompiled database in the absence of a file.

If file is specified, dircolors reads it to determine which colors to use for which file types and extensions. Otherwise, a precompiled database is used. For details on the format of these files, run ‘dircolors --print-database’.

In order to change the settings for everybody, you could create a /etc/dircolors file and add the following to /etc/bashrc:

d=/etc/dircolors
test -r $d && eval "$(dircolors $d)"

Linux set console background colors with dircolors:

Your dircolors file controls the colors for words that appear through ls on the console. Find this .dircolors file for your distribution, a link to help:

http://www.linuxfromscratch.org/blfs/view/svn/postlfs/profile.html

For me on Fedora 17, my dircolors file is: /etc/DIR_COLORS

Copy /etc/DIR_COLORS into your /home/el/.dircolors directory. Create it if it doesn't exist.

Edit /home/el/.dircolors, look for the text "dir".

Change this:

DIR 01;34   # directory

To this:

DIR 01;36   # directory

Save and close and restart the shell. The directories go from dark blue on black (unreadable) to bright teal on black (readable).

  • 2
    -1: no /etc/DIR_COLORS in OP's distro, Debian. And he already knows about ~/.dircolors, that was not the question – MestreLion Jan 29 '15 at 8:01

Your Answer

 

By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Not the answer you're looking for? Browse other questions tagged or ask your own question.