
If you’ve run the ls
command in Bash, you’ll notice that the directories and files you see are colorized according to their type. You can customize your own color scheme to choose different text colors, background colors, and formatting like bold and underline.
How This Works
The color scheme is stored in the LS_COLORS variable. To view your current color scheme, you can tell the Bash to print the contents of the variable:
echo $LS_COLORS
You’ll see a long list of file types and number codes. We’ll explain how to create a list like this yourself.
Before playing around with this, we recommend saving the current contents of the LS_COLORS variable to another variable. This will allow you to quickly restore the default settings without signing out of the shell and signing back in, or closing and reopening the terminal window. To save the current content of the LS_COLORS variable to a new variable named ORIGINAL, run:
ORIGINAL=$LS_COLORS
At any time, you can run the following command to undo your changes and restore the default colors:
LS_COLORS=$ORIGINAL
Your changes are always temporary until you edit a file to make them your new defaults. You can always sign out and sign back in or close and reopen a terminal window to restore the colors to their default setting. However, this makes it easy to do so with a single, quick command.
How to Set Custom Colors
The LS_COLORS variable contains a list of file types along with associated color codes. The default list is long because it specifies different colors for a number of different file types.
Let’s start a basic example to demonstrate how this works. Let’s say we want to change the color of directories from the default bold blue to bold red. We can run the following command to do so:
LS_COLORS="di=1;31"
The di=1;31
bit tells ls
that directories (di
) are (=
) bold (1;
) red (31
).
However, this is just a very simple LS_COLORS variable that defines directories as one color and leaves every other type of file as the default color. Let’s say we want to make files with the .desktop file extension an underlined cyan color, as well. We can run the following command to do so:
LS_COLORS="di=1:31:*.desktop=4;36"
This tells ls
that directories (di
) are (=
) bold (1;
) red (31
) and (:
) any file ending in .desktop (*.desktop
) is (=
) underlined (4;
) cyan (36
).
This is the process for assembling your list of file types and colors. Specify as many as you like in the form filetype=color, separating each with a colon (:) character.
To assemble your own list, you’ll just need to know the list of color codes and file type codes. This uses the same numerical color codes you use when changing the color in your Bash prompt.
…
The post How to Change the Colors of Directories and Files in the ls Command appeared first on FeedBox.