My distro is RHEL. The LS_COLORS variable is made up of things like rs, di, *.tzo, etc. I was wondering how I could add a new extension type.

Similarly in what order are the extensions processed. If di is set to blue, and I make an extension called say zz that does logic to figure out what it means to be a "zz" type, will one go after the other?

  • See /etc/DIR_COLORS for examples – Andy Dalton Jul 13 '18 at 20:54

You should use dircolors to help you personalise the colours used by ls.

dircolors -p > dircolors.txt

will output the colour database in a more-or-less human-readable form and store it in dircolors.txt, which you can then edit to adjust the settings. You’ll see a series of colour settings for “basic file types”, which correspond to di etc.; you can change the colours associated with these but you can’t add your own types. You’ll also see files extensions (starting with .) and the corresponding colours; you can add your own extensions here. Once you’ve finished, run

dircolors dircolors.txt

to produce the corresponding LS_COLORS value, which you should set in your shell’s startup scripts. The order of extensions doesn’t really matter since you should only have one colour setting per extension.

You can also store the colour definitions directly if you prefer, and run

eval $(dircolors -b /path/to/dircolors.txt)

in your startup scripts. (If you use this approach it might make more sense to store the colours in ~/.dircolors.)


To expand on my comment above, you an create a file like /etc/DIR_COLORS in your home directory, maybe ~/.dir_color, then in your .bashrc file (assuming that you're using bash), you can add:

if [[ -e "${HOME}/.dir_colors" ]]; then
    eval "$(dircolors -b ${HOME}/.dir_colors)"

That will set LS_COLORS based on the configuration in .dir_colors. You can tune ~/.dir_colors however you'd like. If you change it, you'll need to re-source your .bashrc:

. ~/.bashrc
  • This will only work on systems which already have /etc/DIR_COLORS; mine don’t, I’m not sure which distributions ship that by default. – Stephen Kitt Jul 13 '18 at 21:03
  • @StephenKitt That's not true, this doesn't require /etc/DIR_COLORS (other than the part where you could use that as a reference). – Andy Dalton Jul 13 '18 at 21:08
  • It’s a bit difficult for Joe Random User to “create a file like /etc/DIR_COLORS” if there’s no such file to serve as reference. – Stephen Kitt Jul 13 '18 at 21:10
  • @StephenKitt If Joe Random User doesn't have /etc/DIR_COLORS, he can surely use Google to get a representative example, or use dircolors -p as you suggest. – Andy Dalton Jul 13 '18 at 21:14

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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