1. Overview

The ls command is a widely used utility in Unix-based operating systems that allows users to list directory contents. By default, ls outputs a simple list of filenames and directories with no visual distinction between different file types or attributes.

In this tutorial, we’ll explore how to change the color of files and directories in the ls command. We’ll also look at assigning distinct colors to various file types and attributes. This enables users to rapidly distinguish between different file categories in a directory.

2. Understanding the dircolors Utility

Customizing the colors of files and directories in the ls output can significantly enhance the readability and structure of directory listings. It’s particularly important when dealing with a directory containing numerous files or having a complex directory structure. By using a customized color scheme, it’s easier to quickly identify file types within a directory and differentiate between them.

Before customizing the colors of files and directories in the ls output, it’s necessary to check if the dircolors utility is already installed on our system:

$ dircolors --version
dircolors (GNU coreutils) 8.22
Copyright (C) 2013 Free Software Foundation, Inc.
License GPLv3+: GNU GPL version 3 or later <http://gnu.org/licenses/gpl.html>

If dircolors is not installed on our system, we can install it using its package manager. On yum-based systems, we can install dircolors with the following command:

$ sudo yum install coreutils

On apt-based systems, we can run the following command to install dircolors:

$ sudo apt-get install coreutils

Let’s generate a default configuration file using the –print-database option of the dircolors command:

$ dircolors --print-database > ~/.dir_colors

The dircolors command in Linux sets the color scheme for files and directories displayed by the ls command. In addition, the –print-database option tells dircolors to output the default color database to the terminal.

To demonstrate, let’s examine the .dir_colors file:

TERM Eterm
TERM ansi
...
DIR 01;34 # directory
LINK 01;36 # symbolic link. (If you set this to 'target' instead of a
.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
 # numerical value, the color is as for the file pointed to.)
MULTIHARDLINK 00 # regular file with more than one link
...
.spx 00;36
.xspf 00;36

In the above dir_colors file, each category has a specific color code. Furthermore, by changing the color code associated with an entity, we can change the color of the directory or file.

Importantly, each line should start with the file type name, followed by a colon and a space, and then the color code.

3. Customizing the Color Scheme

The dircolors utility permits users to customize the ls color scheme. Additionally, it reads a configuration file, typically located at ~/.dir_colors, and assigns specific colors to different file types and attributes.

Furthermore, customizing the colors of directories and files in the ls output can enhance readability and improve visual organization.

3.1. Understanding the Color Codes

It’s important to understand the different variables and color codes used in the configuration file to customize the color scheme effectively.

The variable di corresponds to directories, and its default color code is “01;34”, which represents blue text on a black background.

Furthermore, some common file types and their default color codes include directories “di: 01;34”, executable files “ex: 01;32”, symbolic links “ln: 01;36”, and compressed files “*.gz: 01;31”.

3.2. Editing the Configuration File

We can customize the color scheme for the ls command by editing the ~/.dir_colors configuration file.

We can also assign new colors to different file types and attributes to apply our customizations. The configuration file consists of variable names and color codes. Each variable corresponds to a file type or attribute, and each color code represents a specific color.

Before we edit the ~/.dir_colors file, let’s run the ls -lsh command to look at the colors of the files and directories:

dircolors blue

In the above image, we can see that the currently the color of the directory is blue. To illustrate, let’s update the color code for the directories in the ~/.dir_colors file:

FILE 32 # regular file
DIR 01;31 # directory

In the above configuration, we changed the directory’s color code from “01;34” to “01;31” and the file’s color code from 00 to 32.

3.3. Applying the Changes

After editing the configuration file to customize the color scheme, we reload the file to apply the changes:

$ eval $(dircolors ~/.dir_colors)

Running the above command applies the color configuration specified in ~/.dir_colors to the ls command. Hence, the ls output will display the configured colors for different types of files and directories.

3.4. Verifying the Custom Colors

Let’s check out the color code changes by running ls -lsh within the same directory:

dircolors red

In the above image, we can see that the directory color changed from blue to red, and the file’s color changed to green.

4. Conclusion

In this article, we explored how to change the color of files and directories in the ls command.

First, we looked into the basic concept of dircolors. After that, we updated the colors of files and directories in the ls command output using the dircolors utility.

2 Comments
Oldest
Newest
Inline Feedbacks
View all comments
Comments are open for 30 days after publishing a post. For any issues past this date, use the Contact form on the site.