Changing Colour of Files/Directories in Linux ‘ls’


A default colour set is available in Debian for many file types. A quick look at ~/.bashrc shows that we can uncomment a few lines to enable the colouring of directories and files by extension.

The relevant lines in my ~/.bashrc are,

export LS_OPTIONS='--color=auto'
eval "dircolors"
alias ls='ls $LS_OPTIONS'
alias ll='ls $LS_OPTIONS -l'
alias l='ls $LS_OPTIONS -lA'

The command dircolors outputs the line needed to put the colour information into your environment space. A man dircolor revealed the ability to edit its output.

Using dircolors --print-database you can get the list of colours in an editable form, so if we pipe that to a file,

dircolors --print-database > /etc/dircolors

We can edit this file name and add the colors we want. The file has some explanations as to what the numbers represent.

I added a couple of lines so that .7z and .rar files appear the same as the other compressed archives did.

.tar 01;31 # archives or compressed (bright red)
.gz 01;31
.bz2 01;31
.deb 01;31
.rpm 01;31
.rar 01;31 # added
.7z 01;31 # added

Following this change we need to edit the ~/.bashrc file again to use this new configuration file rather than the default in-built database. Just edit the line beginning with “eval” so it reads,

eval "dircolors /etc/dircolors"

Now you’ll find the new file extensions coloured how you want them. 🙂

Leave a Reply

You may leave the Name and Email fields blank to post anonymously.