How to colourize your directories on FreeBSD using tcsh shell

As I explained in an earlier post I have only just recently switched back to using FreeBSD on my desktop so I find myself having to get reacquainted with it somewhat now. One thing that I found missing after my installation of FreeBSD was the ability for the ls command to show colourized directories or coloured listings of files.  So I went looking to refresh my memory by using a search engine and I eventually found the answer on how to do it after a few false starts.  However, through this process I eventually realized and reminded myself I could have saved myself a lot of time by just reading the very good documentation already provided in the man pages.

Here is what I should have done.  I should have entered the following command and read the manual page thoroughly:

With that I would have received the following manual page below spelling out all the proper options to use for the ls command.  With a bit of reading I would have realized that all I had to do was use the man command and a bit of reading and I would have found that the option -G to the ls command was what would have solved my problem.

Excerpt of FreeBSD ls man page

The Quick Answer

So the short simple answer to my question how do I colourize the directories on FreeBSD using the tcsh shell is to use ls -G command and directories should now be coloured as you can see from this screenshot.

ColourizedDirectories
Result of performing ls -G command

More creatively, if you want to save yourself typing the option out everytime one can create an alias like so in the .cshrc file in your home directory as follows:

The More Complicated Answer

However, I wasn’t satisfied with the default colours chosen by the ls -G command (as you can see from the screenshot above).  But reading further down in the man page I found the answer to tweaking the colours. I will excerpt the relevant portion of the ls man page:

Armed with this extra bit of knowledge about the different ansi colours and the default format "exfxcxdxbxegedabagacad" for the LSCOLORS environment variable I could probably have easily done a quick google search to get a better understanding of how use the format to achieve what I wanted.

Obviously a Google search gave me the answer after several false starts but it would have been more productive to read the manual page first.  In the end the more involved solution of tweaking the ls environment variables in the tcsh shell is what I needed.  I chose more vibrant colours by applying a brighter cyan to display directories and a brighter red for executables.

To arrive at this ideal solution entails modifying and adding two environment variables CLICOLOR and LSCOLORS to the .cshrc file.  As you can see below from the code you need to preface each variable with setenv when using the tcsh shell.  If you were using bash as your shell you would need to export each of those variables in your bash profile.

How to modify .cshrc to display colourized listings on Freebsd

Tweaking the LSCOLORS environment variable I was able to use more vibrant colours for my listings. In the example below I applied a brighter cyan for the directories and a brighter red for the executables.

colourizeddirectories2

Using Linux Default Colour Pattern

If you really don’t want to play with choosing your own colours you can just use the defaults provided by most linux distros as follows:

 

The setting for the LSCOLORS environment variable seems a bit daunting when you come across it in the manual and may be a bit cryptic.  But I found a very useful web site that helps to visualize and refine your colour choices and give you the right codes to use for each file/directory type when you are done.  As with everything you can play around with it until it looks the way you want.

Ultimately the moral of this story for me is that searching the internet for the solution helps but when it comes to BSD the man pages are excellent and packed with all the information you need.  It just takes a different mindset to get used to searching there first which I learned the hard way.  In the end I learned whenever I am in doubt about something with BSD I have to remember to always read the man page for the command first and then search the web for what I am having trouble understanding.

 

One Reply to “How to colourize your directories on FreeBSD using tcsh shell”

Leave a Reply

Your email address will not be published. Required fields are marked *