Colors for ls Mean – How to Change Colors for ls in Bash

Last updated: November 27, 2022

ls is a command from Nix and BSD-based operating systems that enables a user to list all the available files and directories found within a given directory. 

Although you can add certain parameters to expand the displayable information in a terminal using the ls command, there is, however, a more straightforward and simpler way to discern the listed items using color coding techniques.

ls colors meaning

The default setup when executing the ls command in your Linux machine already displays a certain amount of visual and color detail. So, what do the different colors in the ls command mean?

Below is a summary of the color scheme displayed by the ls command, particularly found in the Ubuntu system:

  • Blue: Directory or folders
  • Green: Executable or recognized data file
  • Cyan or Sky Blue: Symbolic link file
  • Yellow with black background: Device
  • Magenta or Pink: Graphic image file
  • Red: Archive file
  • Red with black background: Broken link

In this example, you can see the output of the default colors being displayed by the ls command. The compressed files are shown in red while folders or directories are shown in the default bold blue.

Ubuntu default ls color for files and directory

Turning ON/OFF colors for ls command

There are two ways in which you can turn off the assigned colors on the ls command. The first involves examining the alias associated with the ls command. To know what alias the ls command is using, issue the command:

alias | grep -i ls

In this example, you can see the output that ls has an alias for at least four commands.

To remove the color in the alias, execute the command:

unalias ls
remove ls color using unalias

The second way to turn off the colors in alias is to set the value to none. This is only a temporary measure to check the effect of turning the colors to none. You can do this by executing the command:

ls --color=none
set ls color to none

From the above illustrations, returning the default colors of your ls command should be straightforward. You just set the alias again by following the command alias ls='ls --color=auto'. 

Understanding LS_COLORS

LS_COLORS is an environment variable in which the ls command gets its settings from. To generate a print from the environment variable, the command dircolors provides an easy way to examine the color coding scheme from the LS_COLORS variable. But you can also just echo the value of LS_COLORS.

To display the output colors, execute the command:

dircolors
echo $LS_COLOR
output of dircolors


LS_COLOR Syntax

The color code syntax consists of four parts. Take the following example for a better understanding:

di=1;33;47

The first part represents the keys that refer to the particular item being listed, such as directories, sockets or just about any extension.

The second part after the keys represents the text style.

The third part is the color and the fourth is the background color.

A summary of the values are represented below:

LS_COLORS key value

KeyDescriptionNotes
noNORMAL, NORMGlobal default
fiFILENormal file
diDIRDirectory
lnSYMLINK, LINK, LNKSymbolic link
piFIFO, PIPENamed pipe
doDOORDoor
bdBLOCK, BLKBlock device
cdCHAR, CHRCharacter device
orORPHANSymbolic link pointing to a non-existent file
soSOCKSocket
suSETUIDFile that is setuid (u+s)
sgSETGIDFile that is setgid (g+s)
twSTICKY_OTHER_WRITABLEDirectory that is sticky and other-writable (+t,o+w)
owOTHER_WRITABLEDirectory that is other-writable (o+w) and not sticky
stSTICKYDirectory with the sticky bit set (+t) and not other-writable
exEXECExecutable file (i.e. has 'x' set in permissions)
miMISSINGNon-existent or missing file pointed to by a symbolic link (visible when you type ls -l)
lcLEFTCODE, LEFTOpening terminal code
rcRIGHTCODE, RIGHTClosing terminal code
ecENDCODE, ENDNon-filename text
*.extensionFILE TYPE CODEAny file with any extension

Effects

CodeProperty
00Default color
01Bold font
04Underlined
05Flashing text
07Reverse
08Concealed

Colors

CodeProperty
30Black
31Red
32Green
33Orange
34Blue
35Purple
36Cyan
37Grey


Background Colors

CodeProperty
40Black background
41Red background
42Green background
43Orange background
44Blue background
45Purple background
46Cyan background
47Grey background


Additional Colors

CodeProperty
90Dark grey
91Light red
92Light green
93Yellow
94Light blue
95Light purple
96Turquoise
97White
100Dark grey background
101Light red background
102Light green background
103Yellow background
104Light blue background
105Light purple background
106Turquoise background
107White background

How to change ls colors

You can change the color being displayed by affixing the environment variable at the end of your bashrc file. You can temporarily verify by using running export LS_COLORS="key=colorcode1;colorcode2" command.

Before making any changes to the bashrc, first take the backup of this file by copying its configurations to any other file.

cp -p .bashrc .bashrc.bak

Now let's open up the bashrc file using the vim command.

vim .bashrc

For example, affix the following value to set different colors for files with different extensions:

LS_COLORS=$LS_COLORS:"*.pdf=0;34":"*.txt=01;31;46"

Then save your .bashrc file and source it.

source .bashrc
new color for all files with pdf extensions

In this example, we’ve changed files ending with pdf to have a blue color for its text and all txt files with red on a cyan background

Conclusion

The versatility of ls has improved over the years not just as a tool for listing files and directories but has evolved to become as relevant as ever, providing color coding schemas, displaying file attributes or rearranging lists, all the while being customizable.

As open-source tools have become readily available publicly, some tools like exa have also stepped in to innovate the Coreutils function. According to its official description, exa is an improved file lister program with more features and better defaults.

SHARE

Comments

Please add comments below to provide the author your ideas, appreciation and feedback.

Leave a Reply

Leave a Comment