How to Change the Colors of Directories and Files in the ls Command

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.

RELATED: How to Customize (and Colorize) Your Bash Prompt

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.

Here’s the list of color codes for foreground text:

  • Black: 30
  • Blue: 34
  • Cyan: 36
  • Green: 32
  • Purple: 35
  • Red: 31
  • White: 37
  • Yellow: 33

For example, since yellow text is color code 33, you’d use di=33 to make directories yellow.

Here’s the list of text color attributes:

  • Normal Text: 0
  • Bold or Light Text: 1 (It depends on the terminal emulator.)
  • Dim Text: 2
  • Underlined Text: 4
  • Blinking Text: 5 (This does not work in most terminal emulators.)
  • Reversed Text: 7 (This inverts the foreground and background colors, so you’ll see black text on a white background if the current text is white text on a black background.)
  • Hidden Text: 8

When specifying an attribute or more than one color code, separate the list of codes with a semicolon (;) character. You don’t need to specify 0 for normal text, as normal text is used when you don’t specify an attribute here.

For example, since bold text is color code 1 and yellow text is color code 33, you’d use di=1;33 to make directories bold yellow. You can also specify more than one attribute. For example, you could use di=1;4;33 to make directories bold, underlined yellow.

Here’s the list of background color codes:

  • Black background: 40
  • Blue background: 44
  • Cyan background: 46
  • Green background: 42
  • Purple background: 45
  • Red background: 41
  • White background: 47
  • Yellow background: 43

For example, since a blue background is color code 44, you’d use di=44 to use a blue background for directories. You can also combine a background color code, a foreground color code, and as many attributes as you like. For example, di=1;4;33;44 would give you bold, underlined yellow text on a blue background.

Here’s the list of file type codes:

  • Directory: di
  • File: fi
  • Symbolic Link: ln
  • Named Pipe (FIFO): pi
  • Socket: so
  • Block Device: bd
  • Character Device: cd
  • Orphan Symbolic Link (points to a file that no longer exists): or
  • Missing File (a missing file that an orphan symbolic link points to): mi
  • Executable File (has the “x” permission): ex
  • *.extension: Any file ending with an extension you specify. For example, use *.txt for files ending in .txt, *.mp3 for files ending in .mp3, *.desktop for files ending in .desktop, or anything else you like. You can specify as many different file extensions as you like.

Specify as many different types of file type codes with as many different colors as you like, separated by the : character. Repeat this process to assemble your LS_COLORS variable.

For example, let’s say you want to use bold purple text for directories, underlined red text for executable files, and bold green text on a red background for .mp3 files. Putting together the file type codes and color codes from the lists above, you’d get:

LS_COLORS="di=1;35:ex=4;31:*.mp3=1;32;41"

How to Set Your New Default Colors

You now have a custom LS_COLORS variable that functions in the current Bash session. However, you probably want to make it permanent so it’s automatically used whenever you start a new Bash session without you having to remember this.

You can set your custom LS_COLORS variable—and any other Bash variable you like—by adding it to your user account’s .bashrc file. This file is located at ~/.bashrc. So, if your username is bob, you’ll find it at /home/bob/.bashrc. There are other ways to set environment variables as well, but this is a simple one.

First, open this file in your preferred text editor. We’ll use nano here as an example, but you can use vi, emacs, or anything else you like.

nano ~/.bashrc

Add your custom LS_COLORS variable to a new line at the end of the file, like so:

LS_COLORS="di=1;35:ex=4;31:*.mp3=1;32;41"

Save the file and exit. In nano, press Ctrl+O and then press Enter to save, then press Ctrl+X to exit.

Whenever you start a new Bash session, Bash will read the .bashrc file and automatically set your LS_COLORS variable. To change your colors in the future, go back to your .bashrc file and edit the LS_COLORS line.

You can also just delete the LS_COLORS= line you added to your .bashrc file to use the default colors again. If you don’t set the LS_COLORS value, Bash will use the default colors.

Chris Hoffman is a technology writer and all-around computer geek. He's as at home using the Linux terminal as he is digging into the Windows registry. Connect with him on Twitter.