Linux dircolors Command with Practical Examples

LinuxLinuxBeginner
Practice Now

Introduction

In this lab, you will learn how to use the dircolors command in Linux to customize the color scheme for directory and file listings in the terminal. The dircolors command reads a configuration file that specifies the color settings for different file types and directory types, typically located at ~/.dircolors or /etc/DIR_COLORS. You will start by understanding the purpose of the dircolors command, then learn how to customize the directory and file colors, and finally manage the dircolors configuration files.

Linux Commands Cheat Sheet


Skills Graph

%%%%{init: {'theme':'neutral'}}%%%% flowchart RL linux(("`Linux`")) -.-> linux/BasicFileOperationsGroup(["`Basic File Operations`"]) linux(("`Linux`")) -.-> linux/BasicSystemCommandsGroup(["`Basic System Commands`"]) linux(("`Linux`")) -.-> linux/FileandDirectoryManagementGroup(["`File and Directory Management`"]) linux(("`Linux`")) -.-> linux/VersionControlandTextEditorsGroup(["`Version Control and Text Editors`"]) linux/BasicFileOperationsGroup -.-> linux/cat("`File Concatenating`") linux/BasicSystemCommandsGroup -.-> linux/echo("`Text Display`") linux/FileandDirectoryManagementGroup -.-> linux/cd("`Directory Changing`") linux/BasicFileOperationsGroup -.-> linux/ls("`Content Listing`") linux/VersionControlandTextEditorsGroup -.-> linux/nano("`Simple Text Editing`") subgraph Lab Skills linux/cat -.-> lab-422639{{"`Linux dircolors Command with Practical Examples`"}} linux/echo -.-> lab-422639{{"`Linux dircolors Command with Practical Examples`"}} linux/cd -.-> lab-422639{{"`Linux dircolors Command with Practical Examples`"}} linux/ls -.-> lab-422639{{"`Linux dircolors Command with Practical Examples`"}} linux/nano -.-> lab-422639{{"`Linux dircolors Command with Practical Examples`"}} end

Understand the Purpose of dircolors Command

In this step, you will learn about the purpose of the dircolors command in Linux. The dircolors command is used to customize the color scheme for directory and file listings in the terminal.

The dircolors command reads a configuration file that specifies the color settings for different file types and directory types. This configuration file is typically located at ~/.dircolors or /etc/DIR_COLORS.

Let's start by checking the current color settings on your system:

dircolors --print-database

Example output:

## Configuration file for dircolors, a utility to help you set the
## LS_COLORS environment variable used by GNU ls with the --color option.
## Copyright (C) 1996-2022 Free Software Foundation, Inc.
## Copying and distribution of this file, with or without modification,
## are permitted provided the copyright notice and this notice are preserved.
## The keywords COLOR, OPTIONS, and EIGHTBIT (honored by the
## slackware version of dircolors) are recognized but ignored.

## Below, there should be one TERM entry for each termtype that is colorizable
TERM linux
TERM su
TERM xterm
TERM xterm-color
TERM xterm-debian
TERM rxvt
TERM screen
TERM screen-256color
TERM tmux
TERM tmux-256color
TERM vt100
TERM cons25
TERM fbterm
TERM bay
TERM cygwin
TERM dtterm
TERM dvtm
TERM Eterm
TERM eterm-color
TERM foot
TERM gnome
TERM hurd
TERM jfbterm
TERM kitty
TERM konsole
TERM kterm
TERM lxterminal
TERM st
TERM terminator
TERM tmux-256color
TERM vte
TERM vte-256color
TERM xfce4-terminal
TERM alacritty
TERM alacritty-direct
TERM urxvt
TERM urxvt-256color
TERM screen-256color-bce

## Below are the color init strings for the basic file types. A color init
## string consists of one or more of the following numeric codes:
## Attribute codes:
## 00=none 01=bold 04=underscore 05=blink 07=reverse 08=concealed
## Text color codes:
## 30=black 31=red 32=green 33=yellow 34=blue 35=magenta 36=cyan 37=white
## Background color codes:
## 40=black 41=red 42=green 43=yellow 44=blue 45=magenta 46=cyan 47=white
NORMAL 00 ## global default, although everything should be something.
FILE 00 ## normal file
DIR 01;34 ## directory
LINK 01;36 ## symbolic link
FIFO 33 ## pipe
SOCK 01;35 ## socket
DOOR 01;35 ## door
BLK 01;33 ## block device driver
CHR 01;33 ## character device driver
ORPHAN 01;05;37;41 ## orphaned symlinks
MISSING 01;05;37;41 ## ... and the files they point to
EXEC 01;32 ## executable file

The output shows the current color settings for different file types and directory types. You can customize these settings by modifying the ~/.dircolors configuration file.

In the next step, you will learn how to customize the directory and file colors using the dircolors command.

Customize Directory and File Colors

In this step, you will learn how to customize the colors for directories and files using the dircolors command.

First, let's create a custom .dircolors configuration file in your ~/project directory:

nano ~/.dircolors

Add the following content to the file:

## Custom dircolors configuration
NORMAL 00
FILE 00
DIR 01;32
LINK 01;36
EXEC 01;33

This configuration sets the following color settings:

  • Normal files: default color (00)
  • Directories: bold green (01;32)
  • Symbolic links: bold cyan (01;36)
  • Executable files: bold yellow (01;33)

Save and exit the file.

Now, let's apply the new color settings:

eval $(dircolors ~/.dircolors)

You should now see the directory and file colors change in your terminal.

To make the changes permanent, you can add the eval $(dircolors ~/.dircolors) command to your ~/.bashrc or ~/.zshrc file, depending on your shell.

Let's verify the changes:

ls -l ~/project

Example output:

total 0
drwxr-xr-x 2 labex labex 4096 Apr 18 12:34 [1;32mdirectory[0m
-rw-r--r-- 1 labex labex    0 Apr 18 12:34 [0mfile.txt[0m
lrwxrwxrwx 1 labex labex    5 Apr 18 12:34 [1;36msymlink[0m -> file.txt
-rwxr-xr-x 1 labex labex    0 Apr 18 12:34 [1;33mexecutable[0m

As you can see, the directory, symbolic link, and executable file are now displayed in the custom colors you set in the .dircolors file.

Manage dircolors Configuration Files

In this final step, you will learn how to manage the dircolors configuration files.

The dircolors command reads configuration files to determine the color settings for directory and file listings. The default configuration file is located at /etc/DIR_COLORS, but you can also use a custom configuration file in your home directory (~/.dircolors).

Let's explore the different ways to manage the dircolors configuration files:

  1. View the system-wide configuration file:
cat /etc/DIR_COLORS

This file contains the default color settings for the system.

  1. Create a custom configuration file:
nano ~/.dircolors

You can create a custom configuration file in your home directory (~/.dircolors) and override the system-wide settings.

  1. Apply the custom configuration:
eval $(dircolors ~/.dircolors)

After creating the custom configuration file, you need to apply the changes using the eval command.

  1. Make the custom configuration permanent:
echo 'eval $(dircolors ~/.dircolors)' >> ~/.bashrc

To make the custom configuration permanent, you can add the eval command to your shell's startup file (e.g., ~/.bashrc or ~/.zshrc).

Now, let's verify that the custom configuration is being used:

ls -l ~/project

The directory and file colors should reflect the custom settings you defined in the ~/.dircolors file.

Summary

In this lab, you will learn about the purpose of the dircolors command in Linux, which is used to customize the color scheme for directory and file listings in the terminal. You will also learn how to customize directory and file colors, as well as manage the dircolors configuration files. The lab covers understanding the purpose of the dircolors command, customizing directory and file colors, and managing the dircolors configuration files.

Linux Commands Cheat Sheet

Other Linux Tutorials you may like