How to Change Colors on LS in Bash


If you work on the command line interface of Linux most of the time, then changing the color of ls command might be something that you always wanted. Perhaps you don’t like the default colors or they are hard to read for your eyes.

If you’re using a Linux graphical desktop environment such as GNOME 3 or KDE 5 Plasma, you can just change the theme of your Terminal application such as GNOME Terminal or Konsole. But if you’re using a server operating system like Ubuntu Server that don’t have any graphical user interface, then changing themes like you do in a graphical Terminal application is not easy.

In this article, I will show you how to change the colors of ls command. I am going to use Ubuntu Desktop and Server for the demonstration. But you can use any Linux distribution of your choice. Let’s get started.

Enable Colors of ls Command

By default, on most Linux distributions these days has ls colors enabled. If your Linux distribution is an exception to this default setting, then you may run ls with –color option to enable colored output.

The LS_COLORS Environment Variable

LS_COLORS environment variable is responsible for the colors that you see when you run the ls command.

You can print the LS_COLORS variable with the following command and see how the contents of the LS_COLORS environment variable looks like.

$ echo $LS_COLORS

To change the colors, what you usually do is change these key value pairs and update the LS_COLORS environment variable.

Exporting, Editing and Updating the LS_COLORS Environment Variable

Let’s first see how to export the LS_COLORS variable.

You can run the following command to export LS_COLORS variable to the end of your ~/.bashrc file:

$ dircolors -b >> .bashrc

Now edit the ~/.bashrc file with any text editor. I am going to use vim text editor.

$ vim ~/.bashrc

Once the file is opened. Go to the end of the file. You should see something like the marked section of the screenshot below.

What you want to do is, edit the value of specific key, or add new key value pair to the end of the LS_COLORS environment variable. Then save the file and run the following command to apply the new settings.

$ source ~/.bashrc

Don’t worry, your changes will survive reboots.

Basics of Terminal Color Codes

In this section, I will talk about how LS_COLORS color codes are formatted. It is a must have knowledge to modify LS_COLORS environment variable.

LS_COLORS key value pairs are separated by colon ( : ). The keys are predefined for the most part. Only the color values change.

The values have 2 or more parts separated by semicolon (;).

For example, di=0;34, here di means the color should be applied to directories. 0 means it’s a normal color, and 34 means the color is green.

If you want bold green font for the directories, the color code should be di=1;34. Here 1 means bold font.

If you also want to specify a background color, you can append the code for it as well. For example, if you want yellow normal font on red background, then the code should be di=1;33;41

List of Available Color Codes:

31  = red 40  = black background 0   = default colour
32  = green 41  = red background 1   = bold
33  = orange 42  = green background 4   = underlined
34  = blue 43  = orange background 5   = flashing text
35  = purple 44  = blue background 7   = reverse field (exchange foreground and background color)
36  = cyan 45  = purple background 8   = concealed (invisible)
37  = grey 46  = cyan background 0   = default colour
90  = dark grey 47  = grey background 1   = bold
91  = light red 100 = dark grey background
92  = light green 101 = light red background
93  = yellow 102 = light green background
94  = light blue 103 = yellow background
95  = light purple 104 = light blue background
96  = turquoise 105 = light purple background
97  = white 106 = turquoise background
107 = white background

List of Some of the Available Keys:

no Global default
fi Normal file
di Directory
ln Symbolic link.
bd Block device
cd Character device
or Symbolic link to a non-existent file
ex Executable file
*.extension Example, *.mp3

Take a look at the links in the References section for more information on the available keys.

Practical Example:

In this section, I will set yellow normal font on red background for directory color.

I edited ~/.bashrc and set di=1;33;41 and saved the file.

I ran source ~/.bashrc command.

Take a look at the magic in the screenshot below.

That’s how you customize the colors used in the ls command. Thanks for reading this article.





ONET IDC thành lập vào năm 2012, là công ty chuyên nghiệp tại Việt Nam trong lĩnh vực cung cấp dịch vụ Hosting, VPS, máy chủ vật lý, dịch vụ Firewall Anti DDoS, SSL… Với 10 năm xây dựng và phát triển, ứng dụng nhiều công nghệ hiện đại, ONET IDC đã giúp hàng ngàn khách hàng tin tưởng lựa chọn, mang lại sự ổn định tuyệt đối cho website của khách hàng để thúc đẩy việc kinh doanh đạt được hiệu quả và thành công.
Bài viết liên quan

Bash `pushd` command

The Linux users may need to switch between many directories for doing a particular task and it is a time consuming task...

BASH Heredoc Tutorial

How to use Here Document in bash programming A block of code or text which can be redirected to the command script or interactive...

30 bash script Interview questions and answers

Bash scripting is a very useful and powerful programming language that is mainly used to make any manual task automated....