Bash – Modify dircolors Settings Globally

bashcolorsls

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.

Best Answer

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!
Related Question