How to Change Colors for ls in Bash [4 Best Methods]

Written by

Reviewed by

Last updated: July 2, 2024

Expert verified

SVG Image

TL;DR

To change colors for ls in Bash, follow these steps:

  1. Check the current configuration with echo $LS_COLORS.
  2. Modify LS_COLORS with export LS_COLORS=”di=01;34:fi=0;37″.
  3. Add the export command to your .bashrc file.
  4. Save the file and reload your terminal with source ~/.bashrc.

Continue reading the guide below to learn different methods to change colors for ls in Bash and common errors that can occur during the process.

Managing files in your terminal can be a hassle without visual aids. Changing the colors of the ls command in Bash can make this task much easier. In this post, I’ll show you how to customize ls colors, fix common errors, and improve your terminal experience. By the end, you’ll have a personalized and efficient setup. Ready to transform your terminal? Let’s explore and make those colors work for you!

What is ls?

The ls command in Linux and Unix-like operating systems stands for list. It is used to list files and directories within the file system. When executed, ls displays the contents of a specified directory or the current working directory if no directory is specified. It provides a quick and efficient way to view and manage files, showing details such as file names, sizes, and modification dates.

Why Customizing ls Color Output Can Improve Productivity

1. Visual Differentiation

        • Quick Identification: Custom colors allow users to quickly distinguish between different types of files (e.g., directories, executable files, symbolic links) at a glance.
        • Error Reduction: Reduced chance of mistakes, such as accidentally modifying the wrong file type, due to clear visual cues.

        2. Enhanced Readability

        • Clarity: Different colors can improve the readability of the directory listings, especially in directories with a large number of files.
        • Focus: Users can highlight specific file types that are relevant to their tasks, allowing for easier navigation and focus on important files.

        3. Personalization

        • Tailored Work Environment: Customizing the color scheme to suit personal preferences can make the working environment more comfortable and efficient.
        • Accessibility: Adjusting colors to meet accessibility needs, such as color blindness adjustments, can make the command output more usable for all users.

        4. Efficiency

        • Speed: Faster identification of file types and statuses can save time, especially for users who frequently navigate complex directory structures.
        • Consistency: Maintaining a consistent color scheme across different systems can help users work more fluidly when switching between environments.

        Default Output

        The default ls output typically uses a standard color scheme defined by the system. For example:

        • Directories: Blue
        • Executable Files: Green
        • Symbolic Links: Cyan
        • Compressed Files: Red

        While this is functional, it may not be optimized for every user’s needs or preferences.

        How to Change Colors for ls in Bash

        To change colors for ls in Bash, you need to modify the LS_COLORS environment variable. Start by using the command dircolors --print-database > ~/.dircolors to generate a default color configuration file. Edit this file with your preferred color codes.

        Then, add eval $(dircolors ~/.dircolors) to your .bashrc file to apply the changes. After saving, reload your terminal with source ~/.bashrc. This setup allows you to customize ls colors for better file differentiation and improved readability.

        Keep reading for the detailed step-by-step guide for 4 different methods to change the color of ls in bash:

        1. dircolors Command

        Enhance your Terminal experience with the power of the dircolors command. This method allows you to configure colors for various file types in ls output, making it easier to distinguish between directories, files, and more. Follow these steps to customize your ls colors:

        1. Open your Terminal window.
        opening terminal 43
        1. If you don’t have dircolors installed, you can easily do so using your package manager. On Ubuntu, execute the following command:
        sudo apt-get install dircolors
        1. This command uses the package manager to install the dircolors utility.
        installing dircolors on ubuntu
        1. Once dircolors is installed, create a custom configuration file using this command:
        dircolors --print-database > ~/.dircolors
        1. The dircolors --print-database command generates the default color configuration, and > ~/.dircolors saves it in a file named .dircolors in the user’s home directory.
        creating dircolors configuration file 1
        1. By default the colors of directories and files look like this:
        default colors of files and directories
        1. Open the ~/.dircolors file in a text editor by running the command:
        sudo nano ~/.dircolors
        1. The file contains a list of file types and their corresponding color codes.
        opening configuration file 2
        1. Now customize the colors according to your preferences. In this case, I am changing color code for directories.
        changing color code in configuration file
        1. Save the changes to your shell profile file and then reload the shell or open a new Terminal window for the changes to take effect.
        changed colors of directories

        2. LS_COLORS Environment Variable

        Customize your ls colors effortlessly by mastering the LS_COLORS environment variable. With this method, you gain the flexibility to define colors for different file types, elevating file organization and efficiency. Customize your ls colors with these simple steps:

        1. To check the current LS_COLORS configuration, run the following command:
        echo $LS_COLORS
        1. This command displays the current LS_COLORS configuration set in the environment.
        checking current LS COLORS configuration
        1. Directly modify LS_COLORS to specify file types and their desired color codes. For example:
        export LS_COLORS="di=01;34:fi=0;37"
        1. This command sets the LS_COLORS environment variable with custom color codes. In this example, di represents directories, fi represents regular files, 01;34 represents the color code for directories (blue), and 0;37 represents the color code for regular files (white).
        directly changing color code of specific file types

        3. Bash Aliases

        Discover the convenience of Bash aliases to tailor your ls output. By creating shortcuts and adding advanced information like file sizes and permissions, this method streamlines your file navigation. Follow these steps to change your ls colors using aliases:

        1. Create an enhanced alias to include additional information, such as file sizes and permissions by running the following command:
        alias ls='ls --color=auto -lh'
        1. This enhanced alias displays ls output in long format with human-readable file sizes and colorizes the output.
        creating enhanced ls alias

        4. Custom Scripts for ls Color Customization

        Embrace ultimate control over your ls colors with custom scripts. This method opens doors to endless possibilities, allowing you to craft a truly unique and tailored Terminal experience that reflects your creativity and workflow. Follow these steps:

        1. Using a text editor, create a Bash script that defines your preferred color scheme for different file types. Inside the script, use ANSI escape codes to specify the colors you want to apply.
        customized ls color script file
        1. Ensure that the script has executable permissions using the chmod command:
        chmod +x custom_ls_colors.sh
        1. This command grants execute permissions to the custom_ls_colors.sh script, allowing you to run it as a standalone executable.
        making script file executable 1
        1. Execute the script to apply your custom colors to ls output:
        ./custom_ls_colors.sh
        1. By running the script, the custom colors defined in the script will be applied to the ls command, enhancing the visual appearance of the file listings in your Terminal.
        executing script file 1

        4 Common Errors When Changing Colors for ls in Bash

        When customizing ls colors in Bash, you might encounter some common issues. Understanding how to troubleshoot these can save time and keep your workflow smooth. Here are four common issues and how to fix them.

        • 🔧 Incorrect Color Codes: If you see unusual colors or no colors at all, check your LS_COLORS settings for typos or invalid codes. Use a reference guide to ensure you use the correct syntax.
        • 🔒 File Permission Issues: Sometimes, changes don’t apply due to file permission problems. Ensure your .bashrc or .bash_profile has the right permissions. Use chmod to adjust permissions if needed.
        • 💻 Terminal Emulator Settings: If your colors don’t display correctly, it might be a terminal emulator issue. Check your terminal settings to ensure it supports color and uses the correct color scheme.
        • ♻️ Changes Not Applied: After editing configuration files, you might not see changes. Make sure to reload your Bash configuration using source ~/.bashrc or restart your terminal to apply the updates.

        To Sum Up

        In this article, I have walked through step-by-step methods to change ls colors in Bash, covering everything from using dircolors and the LS_COLORS environment variable to creating Bash aliases and custom scripts.

        For further learning, I recommend checking out:

        • Tips on sorting ls command by date and time for better file organization.
        • Advanced techniques for using the grep command in Linux to enhance text searches.
        • Methods to display hidden files in Linux to improve file management and navigation.

        Frequently Asked Questions

        How do I revert to the default ls colors after customization?

        To revert to the default ls colors after customization, simply remove or comment out any custom color configurations that you added to the relevant files, such as .dircolors or .bashrc. Once you have removed the custom color settings, save the changes to the file. To apply the changes, reload the shell by either restarting the Terminal or executing the appropriate command, depending on your operating system. This will restore the default ls color scheme, giving you a clean slate to further customize or use the standard colors.

        Is it possible to set different color themes based on the user or host system?

        Yes, it is indeed possible to set different color themes for the ls command based on the user or host system. To achieve this, you can use conditional statements in your shell profile file (e.g., .bashrc). By checking the current user or hostname using environment variables, you can specify different dircolors configurations or aliases based on the conditions. This way, each user or host can enjoy a unique and personalized ls color scheme tailored to their preferences or requirements.

        Can I use hexadecimal color codes instead of ANSI escape codes to customize ls colors?

        While hexadecimal color codes are commonly used for web design and graphics, ls in Bash relies on ANSI escape codes for color customization. Using ANSI escape codes is essential to achieve the desired color changes for ls output. These escape codes provide specific instructions to the Terminal for applying colors to different file types, directories, and more. Therefore, when customizing ls colors through the dircolors configuration or aliases, ensure that you use the appropriate ANSI escape codes for a successful and visually appealing result.

        Ojash

        Author

        Ojash is a skilled Linux expert and tech writer with over a decade of experience. He has extensive knowledge of Linux's file system, command-line interface, and software installations. Ojash is also an expert in shell scripting and automation, with experience in Bash, Python, and Perl. He has published numerous articles on Linux in various online publications, making him a valuable resource for both seasoned Linux users and beginners. Ojash is also an active member of the Linux community and participates in Linux forums.

        Akshat

        Reviewer

        Akshat is a software engineer, product designer and the co-founder of Scrutify. He's an experienced Linux professional and the senior editor of this blog. He is also an open-source contributor to many projects on Github and has written several technical guides on Linux. Apart from that, he’s also actively sharing his ideas and tutorials on Medium and Attirer. As the editor of this blog, Akshat brings his wealth of knowledge and experience to provide readers with valuable insights and advice on a wide range of Linux-related topics.

        Share this article
        Shareable URL
        Prev Post

        3 Effective Methods to Update Fedora Linux to Get the Latest Software

        Next Post

        How to Save and Quit Nano [2 Easy Methods]

        Leave a Reply

        Your email address will not be published. Required fields are marked *

        Read next