Skip to content

Instantly share code, notes, and snippets.

@MicahElliott
Last active December 29, 2025 17:02
Show Gist options
  • Select an option

  • Save MicahElliott/719653 to your computer and use it in GitHub Desktop.

Select an option

Save MicahElliott/719653 to your computer and use it in GitHub Desktop.

Dircolors

Color your ls and tree output based on file and directory names/extensions.

Should support most any 256-color capable terminal.

Usage

Clone this repo. In my case this is:

cd ~/proj
git clone https://github.com/MicahElliott/dircolors

Then put into your ~/.zshrc or equivalent:

export DIR_COLORS="$HOME/proj/dircolors/dir_colors"
eval "$(TERM=xterm dircolors -b $DIR_COLORS)"

Customization

This is something of a starter kit that you can use to create your preferred colors.

There is a script here that lets you iterate quickly on trying new colors. Use it as such:

% test-dircolors
I’m now creating a bunch of temp files for you to look at.

This test is mostly manual, but does create test files for you.
Here’s the test cycle:

 1. Edit your $DIR_COLORS file, or ~/.dircolors.
 2. Run this to update visible colors: eval $(TERM=xterm dircolors -b $DIR_COLORS)
 3. Do a colored ls on ~/tmp/test-dircolors
 4. Rinse and repeat until you’re happy with scheme.

Do this when you’re done: rm -rf /home/mde/tmp/test-dircolors.

History

This started as something I loved and used in ~2002. I eventually turned it into a gist. Then I figured it'd be better as a first-class repo project, so here it is. Since then, other similar things have surfaced. AFAIK, the most well-known is LS_COLORS.

# .dircolors
#
# Configure your console file colors.
#
# Author: Micah Elliott http://MicahElliott.com
#
# This file should sit in your $HOME as .dircolors or .dir_colors,
# depending on your distro.
#
# Configuration file for dircolors, a utility to help you set the
# LS_COLORS environment variable used by GNU ls with the --color option.
#
# IT APPEARS SUSE DOES NOT RESPECT THIS
#
# The keywords COLOR, OPTIONS, and EIGHTBIT (honored by the
# slackware version of dircolors) are recognized but ignored.
# Below, there should be one TERM entry for each termtype that is colorizable
TERM linux
TERM linux-c
TERM mach-color
TERM console
TERM con132x25
TERM con132x30
TERM con132x43
TERM con132x60
TERM con80x25
TERM con80x28
TERM con80x30
TERM con80x43
TERM con80x50
TERM con80x60
TERM xterm
TERM xterm-debian
TERM rxvt
TERM screen
TERM screen-w
TERM vt100
# Below are the color init strings for the basic file types. A color init
# string consists of one or more of the following numeric codes:
# Attribute codes:
# 00=none 01=bold 04=underscore 05=blink 07=reverse 08=concealed
# Text color codes:
# 30=black 31=red 32=green 33=yellow 34=blue 35=magenta 36=cyan 37=white
# Background color codes:
# 40=black 41=red 42=green 43=yellow 44=blue 45=magenta 46=cyan 47=white
NORMAL 00 # global default, although everything should be something.
FILE 00 # normal file
DIR 01;34 # directory
LINK 01;36 # symbolic link. (If you set this to 'target' instead of a
# numerical value, the color is as for the file pointed to.)
FIFO 40;33 # pipe
SOCK 01;35 # socket
DOOR 01;35 # door
BLK 40;33;01 # block device driver
CHR 40;33;01 # character device driver
ORPHAN 40;31;01 # symlink to nonexistent file
# This is for files with execute permission:
#EXEC 01;32 # Don't use bright green with brown background!
EXEC 01;32
# TEST_MARKER
# List any file extensions like '.gz' or '.tar' that you would like ls
# to colorize below. Put the extension, a space, and the color init string.
# (and any comments you want to add after a '#')
# If you use DOS-style suffixes, you may want to uncomment the following:
#.cmd 01;32 # executables (bright green)
#.exe 01;32
#.com 01;32
#.btm 01;32
#.bat 01;32
.tar 01;31 # archives or compressed (bright red)
.tgz 01;31
.arj 01;31
.taz 01;31
.lzh 01;31
.zip 01;31
.z 01;31
.Z 01;31
.gz 01;31
.bz2 01;31
.deb 01;31
.rpm 01;31
.jar 01;31 # archives or compressed (bright red)
# image formats
.jpg 01;35
.png 01;35
.gif 01;35
.bmp 01;35
.ppm 01;35
.tga 01;35
.xbm 01;35
.xpm 01;35
.tif 01;35
.png 01;35
.mpg 01;35
.avi 01;35
.fli 01;35
.gl 01;35
.dl 01;35
.svg 01;35
.ico 01;35
# C/C++ programming
.cpp 0;36
.cxx 0;36
.C 0;36
.cc 0;36
.c 0;36
.esqlc 0;36
.pgc 0;36
.pgcc 0;36
.i 0;36
.ii 0;36
.err 1;42
.h 0;36
.hxx 0;36
.H 0;36
.hh 0;36
.inc 0;36
.hpp 0;36
*tags 1;42
# Lex/Yacc
.l 1;35
.ll 1;35
.y 0;35
.yy 0;35
# Fortran
.f 0;35
.F 0;35
.for 0;35
.FOR 0;35
.f90 0;35
.F90 0;35
.ftn 0;35
.FTN 0;35
.fpp 0;35
.FPP 0;35
# Clojure
.clj 38;5;255;0;48;5;34
# Golang
# .go 38;5;255;0;48;5;34
.go 38;5;232;48;5;87 # :87/d7/ff
.mod 38;5;137 # :87/d7/ff
.sum 38;5;135 # :87/d7/ff
# Common programming languages
.java 0;35
.py 0;35
.pl 0;35
.pm 0;35
.rb 0;35
.hs 0;35
.lhs 0;35
# SQL
.sql 0;36
# Shell
.sh 1;33
.rc 1;33
.bash 1;33
.zsh 1;33
.ksh 1;33
.tcsh 1;33
.csh 1;33
.s 0;33
.t 0;33
# XML/description
.html 0;31
.xml 0;31
.css 0;31
.haml 0;31
.erb 0;31
.xml 0;31
.xslt 0;31
.tmpl 0;31
.ini 0;31
.conf 0;31
.cfg 0;31
.yml 0;31
.yaml 0;31
# Unimportant, nearly hidden.
.o 0;30
.class 0;30
.pyc 0;30
.pyo 0;30
# .mod 0;30
.hsc 0;30
# Stand out!
*bug 0;41
*fix 0;44
*core 1;41
# Informational
*log 1;33
*report 1;33
*.res 1;33
*.df 1;33
# Build
*Makefile 0;31
*makefile 0;31
.mk 0;31
.gmk 0;31
*Makefile.PL 0;31
*Build.PL 0;31
# Documentation
*README 0;33
*INSTALL 0;33
*LICENSE 0;33
.mkd 0;33
.md 0;33
.markdown 0;33
.txt 0;33
.tex 0;33
.moin 0;33
.rst 0;33
# Language
.eo 38;5;255;0;48;5;34
#! /bin/bash
# test-dircolors — Fine-tune your dircolors for unique colorings of any file type
#
# Author: Micah Elliott http://MicahElliott.com
#
# Tools like ls, tree, zsh color completion, etc make use of dircolors
# settings. This script will show you bunches of files, colored by
# their various extensions (or non-extension files like Makefile).
# Useful for tuning your console color scheme and grouping files by
# type. ls’s -X option is a nice way to group your file listings by
# extension.
#
# The philosophy behind this is that certain types of files are
# similar, and it’s nice to be able to see them as such. For example,
# you may want to color scripts in purple,
# Makefiles/Rakefiles/Gemfiles in yellow, logs in red, archives in
# blue, images in brown, configs in green, generated stuff in dark
# gray, and so on. Choose the categories that suit you (you’ve only
# got a handful of colors to work with) and run this repeatedly until
# you’re happy.
#
# See https://gist.github.com/718567 for an example xterm color
# scheme.
base_files="
tags
core
bug
fix
log
report
Makefile
makefile
README
INSTALL
LICENSE
"
ext_files="
.tgz
.lzh
.zip
.bz2
.deb
.rpm
.jar
.jpg
.png
.gif
.bmp
.ppm
.tga
.xbm
.xpm
.tif
.png
.mpg
.avi
.fli
.gl
.dl
.zip
.z
.bz2
.deb
.rpm
.jar
.jpg
.png
.gif
.bmp
.ppm
.tif
.png
.avi
.gl
.dl
.cpp
.cxx
.C
.cc
.c
.esqlc
.pgc
.pgcc
.ii
.err
.h
.H
.hh
.inc
.hpp
.l
.ll
.y
.yy
.f
.F
.for
.F90
.FPP
.java
.py
.hs
.pl
.pm
.rb
.sql
.go
.mod
.sum
.clj
.cljs
.sh
.rc
.bash
.sh
.ksh
.tcsh
.csh
.s
.t
.o
.class
.pyc
.pyo
.mod
.res
.mk
.gmk
.html
.xml
.txt
.mkd
.rst
.moin
.conf
.ini
.cfg
.mp3
.mp4
.ogv
.ogg
.eo
"
tmpdir=$TMPDIR/test-dircolors
echo "I’m now creating a bunch of temp files for you to look at."
mkdir -p $tmpdir
cd $tmpdir
# Extension files
for e in $ext_files; do
touch "The quick brown fox jumps over the lazy dog"$e
done
# Files with no extension/suffix
for b in $base_files; do
touch $b
done
# An executable
touch a
chmod +x a
eval $(TERM=xterm dircolors -b $DIR_COLORS)
#ls --color=always -lBX $tmpdir
#rm -rf $tmpdir
echo
echo "This test is mostly manual, but does create test files for you."
echo "Here’s the test cycle:"
echo
echo ' 1. Edit your $DIR_COLORS file, or ~/.dircolors.'
echo ' 2. Run this to update visible colors: eval $(TERM=xterm dircolors -b $DIR_COLORS)'
echo " 3. Do a colored ls on $tmpdir"
echo " 4. Rinse and repeat until you’re happy with scheme."
echo
echo "Do this when you’re done: rm -rf $tmpdir."
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment