- Unix Commands Reference
- Unix Commands - Home
dircolors Command in Linux
The dircolors command in Linux is used to modify the colors of the ls output. The ls command lists the contents of the directory. It highlights the contents through colors. For example, directories are listed in blue colors, and files are in white. But this default output can be modified using the dircolors.
The ls command by default reads the LS_COLORS environment variable to set the colors for file types. To modify the default behavior, the dircolors command is used.
Table of Contents
Here is a comprehensive guide to the options available with the dircolors command −
Syntax of dircolors Command
The syntax for using the dircolors command is as follows −
dircolors [options] [file]
The [options] field is used to specify the options to format the output. To customize the output, the file can also be specified in the place of the [file] field. This file essentially contains data to modify the LS_COLORS environment variable.
dircolors Command Options
Options that are used with Linux dircolors command are listed below −
Options | Description |
---|---|
-b (--sh , --bourne-shell ) | It displays the LS_COLORS environment variable values for Bash |
-c (--csh, --c-shell) | It displays the LS_COLORS environment variable values for Csh |
-p (--print-database) | It prints the default configuration of the LS_COLORS environment variable |
--print-ls-colors | It prints the LS_COLORS entries on each line in a color they are presented in the output |
--help | It prints the help |
--version | It displays the command version |
Examples of dircolors Command in Linux
The dircolors command is primarily used to modify the LS_COLORS environment variable. The LS_COLORS variable contains the values of colors that are assigned to specific file types and attributes. The attributes specify whether the filename will be displayed as bold, underlined, or fully highlighted with background color.
Customizing the ls command output requires a configuration file. Let’s begin with generating this file −
- Generating the dircolors Configuration File
- Accessing the dircolors Configuration File
- Understanding the Configuration File
- Customizing the ls Output through dircolors Configuration File
- Changing the File Colors
- Changing the Directory Colors
- Changing File Colors by Extension
Generating the dircolors Configuration File
To generate the configuration file to modify the LS_COLORS variable, execute the dircolors command with the -p flag and direct the output to a .dir_colors file.
dircolors -p > ~/.dir_colors
In the above command, the ~ indicates the /home/user directory while the dot before the filename hides it. The dircolors command output will be stored in the dircolors file in the home directory.
Accessing the dircolors Configuration File
To open the configuration file generated by the dircolors command, use any text editor. To open it in the nano text editor, run −
sudo nano ~/.dir_colors
The configuration file contains file types and their respective default colors in the form of codes.
Understanding the Configuration File
In the configuration file, you will notice that files are attributed with a code of three or two values separated by a semicolon (;).
The first value signifies the background color of the filename, the second foreground, and the third text attribute. The sequence of codes is flexible; they can be provided in any order, and using one or two codes is sufficient, not necessarily all three. The codes themselves indicate whether they belong to the background color group, foreground, or text attribute.
The standard text attribute codes are listed below −
Code | Attribute | Description |
---|---|---|
00 | None | The specified filenames will appear in the default white color |
01 | Bold | The specified filenames will be made bold |
04 | Underscore | The specified filenames will be underlined |
05 | Blink | The specified filenames will be blinked |
07 | Reverse | The specified filenames will set the current color as the background color |
08 | Concealed | The specified filenames will be hidden |
The standard text foreground colors are listed below −
Code | Color |
---|---|
00 | No Color |
30 | Black |
31 | Red |
32 | Green |
33 | Yellow |
34 | Blue |
35 | Magenta |
36 | Cyan |
37 | White |
The standard text background colors are mentioned in the following table −
Code | Color |
---|---|
00 | No Color |
40 | Black |
41 | Red |
42 | Green |
43 | Yellow |
44 | Blue |
45 | Magenta |
46 | Cyan |
47 | White |
Customizing the ls Output through dircolors Configuration File
The foreground and background colors mentioned in the previous section are standard but fully customizable. While classic terminals can only display a maximum of 8 colors, modern terminals support a wide range of colors beyond these ANSI colors.
Before customizing the ls command output, it is important to integrate the .dir_colors configuration file into the terminal file. Depending upon the terminal, open its configuration file, and execute the following command −
echo eval $(dircolors ~/.dir_colors) >> ~/.bashrc
This step is important to read the configuration from the dir_colors file.
Note − Replace the .bashrc with the respective terminal configuration files, e.g. zshrc, or cshrc.
Changing the File Colors
The default file color is white. To change it open the .dir_colors file and locate the FILE keyword −
Uncomment the line if it is commented, and add a background, foreground, and text attribute (43;30;01).
Here, 43 (yellow) represents the background color, 30 (black) is the foreground color and 01 (bold) indicates the text attribute. Save the file and exit the editor.
Finally, source the .bashrc file to apply the changes −
source ~/.bashrc
Verify the changes by running the ls command −
Colors of any file type can be customized using this approach.
Changing the Directory Colors
To change the default colors of the directories, open the .dir_colors file and find DIR.
To change its color to magenta, use the 35 as the foreground color code. To underline the directories, change the attribute 01 to 04.
After making these changes save the file and exit the editor.
To verify, run the ls command −
Changing File Colors by Extension
To change the file color with a specific extension, find the extension type in the .dir_colors configuration file and modify the colors. For example, to change the file color with .zip file types, use −
From the file, it can be seen that the default zip file color is 31 (red) with the 01 (bold) attribute.
Let’s add a background, and foreground color to the zip file types −
The 46 (cyan), is the background color, 30 (black) indicates the foreground color, and 01 is a bold attribute. Save the configuration file and source the .bashrc file.
Verify the changes, and execute the ls command −
In this manner, file colors of any extension can be customized.
Conclusion
The dircolors is a Linux command line tool that is used to modify the output of the ls command. Customization can play a vital role in identifying the files in a list. To change the ls command output colors, first, generate a configuration file. Then, configure the .bashrc file to read this configuration file for ls output. After making changes in the configuration file source .bashrc to apply the changes.