Robert Elder Software Inc.
  • Home
  • Store
  • Blog
  • Contact
  • Home
  • Store
  • Blog
  • Contact
  • #linux
  • |
  • #commandline
  • |
  • #softwareengineering
  • |
  • #embeddedsystems
  • |
  • #compilers
  • ...
  • View All >>

Intro To 'dircolors' Command In Linux

2023-12-27 - By Robert Elder

     I use the 'dircolors' command to print information about which colors will be shown in the output from the 'ls' command:

dircolors
LS_COLORS='rs=0:di=01;34:ln=01;36:mh=00:pi=40;33:so=01;35:do=01;35:bd=40;33;01:c
d=40;33;01:or=40;31;01:mi=00:su=37;41:sg=30;43:ca=30;41:tw=30;42:ow=34;42:st=37;
44:ex=01;32:*.tar=01;31:*.tgz=01;31:*.arc=01;31:*.arj=01;31:*.taz=01;31:*.lha=01
;31:*.lz4=01;31:*.lzh=01;31:*.lzma=01;31:*.tlz=01;31:*.txz=01;31:*.tzo=01;31:*.t
7z=01;31:*.zip=01;31:*.z=01;31:*.dz=01;31:*.gz=01;31:*.lrz=01;31:*.lz=01;31:*.lz
o=01;31:*.xz=01;31:*.zst=01;31:*.tzst=01;31:*.bz2=01;31:*.bz=01;31:*.tbz=01;31:*
.tbz2=01;31:*.tz=01;31:*.deb=01;31:*.rpm=01;31:*.jar=01;31:*.war=01;31:*.ear=01;
31:*.sar=01;31:*.rar=01;31:*.alz=01;31:*.ace=01;31:*.zoo=01;31:*.cpio=01;31:*.7z
=01;31:*.rz=01;31:*.cab=01;31:*.wim=01;31:*.swm=01;31:*.dwm=01;31:*.esd=01;31:*.
jpg=01;35:*.jpeg=01;35:*.mjpg=01;35:*.mjpeg=01;35:*.gif=01;35:*.bmp=01;35:*.pbm=
01;35:*.pgm=01;35:*.ppm=01;35:*.tga=01;35:*.xbm=01;35:*.xpm=01;35:*.tif=01;35:*.
tiff=01;35:*.png=01;35:*.svg=01;35:*.svgz=01;35:*.mng=01;35:*.pcx=01;35:*.mov=01
;35:*.mpg=01;35:*.mpeg=01;35:*.m2v=01;35:*.mkv=01;35:*.webm=01;35:*.ogm=01;35:*.
mp4=01;35:*.m4v=01;35:*.mp4v=01;35:*.vob=01;35:*.qt=01;35:*.nuv=01;35:*.wmv=01;3
5:*.asf=01;35:*.rm=01;35:*.rmvb=01;35:*.flc=01;35:*.avi=01;35:*.fli=01;35:*.flv=
01;35:*.gl=01;35:*.dl=01;35:*.xcf=01;35:*.xwd=01;35:*.yuv=01;35:*.cgm=01;35:*.em
f=01;35:*.ogv=01;35:*.ogx=01;35:*.aac=00;36:*.au=00;36:*.flac=00;36:*.m4a=00;36:
*.mid=00;36:*.midi=00;36:*.mka=00;36:*.mp3=00;36:*.mpc=00;36:*.ogg=00;36:*.ra=00
;36:*.wav=00;36:*.oga=00;36:*.opus=00;36:*.spx=00;36:*.xspf=00;36:';
export LS_COLORS

     For example, the colours that you see in the output from this 'ls' command are determined by the output of the 'dircolors' command that you see above:

'ls' Colours

The 'LS_COLORS' Variable & The 'dircolors' Command

     This part of the output from the 'dircolors' command provides shell commands that will initialize an environment variable called 'LS_COLORS':

LS_COLORS='...
...'
export LS_COLORS

     The 'LS_COLORS' variable is used by the 'ls' command, the 'dir' command, and the 'vdir' command.  You can read more about this in the 'info' pages of the 'ls' command:

info ls
...
     Note that using the ‘--color’ option may incur a noticeable
     performance penalty when run in a directory with very many entries,
     because the default settings require that ‘ls’ ‘stat’ every single
     file it lists.  However, if you would like most of the file-type
     coloring but can live without the other coloring options (e.g.,
     executable, orphan, sticky, other-writable, capability), use
     ‘dircolors’ to set the ‘LS_COLORS’ environment variable like this,
          eval $(dircolors -p | perl -pe \
            's/^((CAP|S[ET]|O[TR]|M|E)\w+).*/$1 00/' | dircolors -)
     and on a ‘dirent.d_type’-capable file system, ‘ls’ will perform
     only one ‘stat’ call per command line argument.
...

Extracting The 'LS_COLORS' Values

     I can pipe the contents of the LS_COLORS variable into this 'tr' command to see the individual rules:

echo "$LS_COLORS" | tr ':' '\n'
rs=0
di=01;34
ln=01;36
mh=00
pi=40;33
so=01;35
do=01;35
bd=40;33;01
cd=40;33;01
or=40;31;01
mi=00
su=37;41
sg=30;43
ca=30;41
tw=30;42
ow=34;42
st=37;44
ex=01;32
*.tar=01;31
*.tgz=01;31
*.arc=01;31
*.arj=01;31
*.taz=01;31
*.lha=01;31
*.lz4=01;31
...

     The first part (before the '=' symbols) describes which files to match, and the second part (after the '=' symbol) is a fragment of an ANSI color escape sequence.

     I can pipe these lines into this 'sed' command to see a list of the different colours that are used for different file types:

echo "$LS_COLORS" | tr ':' '\n' | sed 's/\([^=]\+\)=\(.*\)/\x1B[\2m\1\x1B[0m/'
dircolors Colours

How 'dircolors' Is Used In '~/.bashrc'

     On my machine, the default '.bashrc' file contains a statement to evaluate the output of the 'dircolors' command:

vi ~/.bashrc
...
# enable color support of ls and also add handy aliases
if [ -x /usr/bin/dircolors ]; then
    test -r ~/.dircolors && eval "$(dircolors -b ~/.dircolors)" || eval "$(dircolors -b)"
    alias ls='ls --color=auto'
    #alias dir='dir --color=auto'
    #alias vdir='vdir --color=auto'

    alias grep='grep --color=auto'
    alias fgrep='fgrep --color=auto'
    alias egrep='egrep --color=auto'
fi
...

How 'dircolors' Defaults Are Specified

     In the GNU Core Utils implementation of the 'dircolors' command, you can find a default hardcoded list of colours.

     You can also specify your own colours in a separate file as illustrated by the following example:

dircolors -p > ~/.dircolors
sed -i 's/[0-9][0-9];[0-9][0-9]/37;41/g' ~/.dircolors
ls
eval "$(dircolors -b ~/.dircolors)"
ls
Changing dircolors

     And that's why the 'dircolors' command is my favourite Linux command.

Intro To 'stty' Command In Linux
Intro To 'stty' Command In Linux
Published 2023-10-04
Terminal Block Mining Simulation Game
$1.00 CAD
Terminal Block Mining Simulation Game
Intro To 'nproc' Command In Linux
Intro To 'nproc' Command In Linux
Published 2023-07-15
Intro To 'comm' Command In Linux
Intro To 'comm' Command In Linux
Published 2023-09-06
How To Force The 'true' Command To Return 'false'
How To Force The 'true' Command To Return 'false'
Published 2023-07-09
A Surprisingly Common Mistake Involving Wildcards & The Find Command
A Surprisingly Common Mistake Involving Wildcards & The Find Command
Published 2020-01-21
A Guide to Recording 660FPS Video On A $6 Raspberry Pi Camera
A Guide to Recording 660FPS Video On A $6 Raspberry Pi Camera
Published 2019-08-01
Intro To 'chroot' Command In Linux
Intro To 'chroot' Command In Linux
Published 2023-06-23
Join My Mailing List
Privacy Policy
Why Bother Subscribing?
  • Free Software/Engineering Content. I publish all of my educational content publicly for free so everybody can make use of it.  Why bother signing up for a paid 'course', when you can just sign up for this email list?
  • Read about cool new products that I'm building. How do I make money? Glad you asked!  You'll get some emails with examples of things that I sell.  You might even get some business ideas of your own :)
  • People actually like this email list. I know that sounds crazy, because who actually subscribes to email lists these days, right?  Well, some do, and if you end up not liking it, I give you permission to unsubscribe and mark it as spam.
© 2025 Robert Elder Software Inc.
SocialSocialSocialSocialSocialSocialSocial
Privacy Policy      Store Policies      Terms of Use