Table of Contents#
- Understanding Terminal Colors in Linux
- Prerequisites
- Step-by-Step Guide to Change Directory Color
- Troubleshooting Common Issues
- Conclusion
- References
Understanding Terminal Colors in Linux#
Before diving into changes, let’s briefly explain how terminal colors work in Linux. Most modern terminals use ANSI escape codes to define text colors, styles (e.g., bold), and backgrounds. For the ls command (used to list directory contents), colors are controlled by the LS_COLORS environment variable.
LS_COLORS is a colon-separated list of key-value pairs, where each key defines a file type or attribute, and the value defines its color/style. For directories, the key is di (short for “directory”). The value is an ANSI color code, often prefixed with style modifiers (e.g., 1; for bold).
Example LS_COLORS Entry for Directories#
A typical default entry for directories looks like:
di=34 Here, di = directory, and 34 is the ANSI code for dark blue (the problematic color we’re fixing).
Prerequisites#
To follow this guide, you’ll need:
- A Redhat-based Linux system (RHEL, CentOS Stream, Fedora, or Rocky Linux).
- Access to a terminal (physical or SSH).
- Basic familiarity with command-line navigation (e.g.,
cd,ls). - Write permissions for your home directory (to modify shell configuration files like
.bashrc).
Step-by-Step Guide to Change Directory Color#
Step 1: Identify Your Current Directory Color Setting#
First, let’s confirm the current color code for directories. Run this command in your terminal:
echo $LS_COLORS | grep "di=" Example Output:
di=34:fi=0:ln=36:pi=33:so=33:bd=33;01:cd=33;01:or=31;01:mi=01;05;37;41:ex=32:*.tar=31:*.tgz=31:... The di=34 part tells us directories are currently set to ANSI code 34 (dark blue).
Step 2: Choose a New Directory Color#
Now, pick a new color that’s more readable on a black background. Below is a table of common ANSI color codes (foreground colors) to help you choose:
| ANSI Code | Color Name | Description | Readability on Black Background |
|---|---|---|---|
| 30 | Black | Dark (not useful here) | Poor |
| 31 | Red | Standard red | Good (if not overused) |
| 32 | Green | Standard green | Excellent |
| 33 | Yellow | Standard yellow | Excellent |
| 34 | Blue | Dark blue (default) | Poor (hard to read) |
| 35 | Magenta | Purple | Good |
| 36 | Cyan | Teal/light blue | Good |
| 37 | White | Light gray | Good |
| 90 | Bright Black | Dark gray | Fair |
| 91 | Bright Red | Light red | Excellent |
| 92 | Bright Green | Light green | Excellent |
| 93 | Bright Yellow | Light yellow | Excellent |
| 94 | Bright Blue | Light blue | Good |
| 95 | Bright Magenta | Light purple | Good |
| 96 | Bright Cyan | Light cyan (teal) | Best (high contrast) |
| 97 | Bright White | White | Good (may be too bright) |
Recommendation: Avoid 34 (dark blue). Instead, try 32 (green), 33 (yellow), or 96 (bright cyan)—these are widely considered the most readable on black backgrounds.
Step 3: Test the New Color Temporarily#
Before making permanent changes, test the new color in your current terminal session. Use this command, replacing NEW_CODE with your chosen ANSI code (e.g., 96 for bright cyan):
export LS_COLORS="di=NEW_CODE:$LS_COLORS" Example (Bright Cyan):
export LS_COLORS="di=96:$LS_COLORS" Now run ls to see the change:
ls Directories should now appear in your chosen color. If you don’t like it, try another code (e.g., 32 for green: export LS_COLORS="di=32:$LS_COLORS").
Step 4: Make the Change Permanent#
To retain the new color across terminal sessions and reboots, add the export command to your shell configuration file. Most Redhat users use the bash shell, so we’ll modify .bashrc (in your home directory).
For Bash Users:#
-
Open
.bashrcwith a text editor (e.g.,nanoorvim):nano ~/.bashrc -
Scroll to the bottom of the file and add the
exportcommand from Step 3. For example, to set bright cyan:# Custom directory color (replace 96 with your chosen code) export LS_COLORS="di=96:$LS_COLORS" -
Save and exit:
- In
nano: PressCtrl+Oto save, thenCtrl+Xto exit. - In
vim: PressEsc, type:wq, and pressEnter.
- In
-
Apply the changes immediately by sourcing
.bashrc:source ~/.bashrc
For Zsh Users (if you use zsh instead of bash):#
If you use zsh, modify ~/.zshrc instead of ~/.bashrc:
nano ~/.zshrc Add the same export line, then source with:
source ~/.zshrc Step 5: Verify the New Color#
Open a new terminal window (or run ls again in the current one). Directories should now display in your chosen color. To confirm the change is permanent, log out and log back in—your new color should persist.
Troubleshooting Common Issues#
Issue 1: Changes Don’t Take Effect#
- Cause: You forgot to source
.bashrcor opened a new terminal before sourcing. - Fix: Run
source ~/.bashrcor close and reopen your terminal.
Issue 2: Color Code Not Working#
- Cause: Typo in the ANSI code (e.g.,
99instead of96). - Fix: Double-check the ANSI code table above. Use
echo $LS_COLORS | grep di=to confirm thedi=entry uses your chosen code.
Issue 3: Other File Types Are Affected#
- Cause: Accidentally overwrote other
LS_COLORSentries (e.g., missing:$LS_COLORSin theexportcommand). - Fix: Ensure your
exportline appends to existingLS_COLORS:export LS_COLORS="di=96:$LS_COLORS" # Correct (appends) # NOT: export LS_COLORS="di=96" # Incorrect (overwrites all other settings)
Conclusion#
Changing the directory color in Redhat Linux is a simple yet impactful tweak that improves readability and reduces eye strain. By following these steps, you’ve customized LS_COLORS to use a brighter, more visible color for directories—permanently, for your user account.
Experiment with different ANSI codes (e.g., 32 for green or 93 for bright yellow) to find what works best for your eyes. Enjoy your newly readable terminal!