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

Channel: Linux
Abstract: 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=

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 KeyDescriptionNotesnoNORMAL, NORMGlobal defaultfiFILENormal filediDIRDirectorylnSYMLINK, LINK, LNKSymbolic linkpiFIFO, PIPENamed pipedoDOORDoorbdBLOCK, BLKBlock devicecdCHAR, CHRCharacter deviceorORPHANSymbolic link pointing to a non-existent filesoSOCKSocketsuSETUIDFile 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 stickystSTICKYDirectory with the sticky bit set (+t) and not other-writableexEXECExecutable 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 codercRIGHTCODE, RIGHTClosing terminal codeecENDCODE, ENDNon-filename text*.extensionFILE TYPE CODEAny file with any extension Effects CodeProperty00Default color01Bold font04Underlined05Flashing text07Reverse08Concealed Colors CodeProperty30Black31Red32Green33Orange34Blue35Purple36Cyan37Grey


Background Colors

CodeProperty40Black background41Red background42Green background43Orange background44Blue background45Purple background46Cyan background47Grey background


Additional Colors

CodeProperty90Dark grey91Light red92Light green93Yellow94Light blue95Light purple96Turquoise97White100Dark grey background101Light red background102Light green background103Yellow background104Light blue background105Light purple background106Turquoise background107White 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.

Ref From: linuxopsys

Related articles