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.
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
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
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
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
Key | Description | Notes |
no | NORMAL, NORM | Global default |
fi | FILE | Normal file |
di | DIR | Directory |
ln | SYMLINK, LINK, LNK | Symbolic link |
pi | FIFO, PIPE | Named pipe |
do | DOOR | Door |
bd | BLOCK, BLK | Block device |
cd | CHAR, CHR | Character device |
or | ORPHAN | Symbolic link pointing to a non-existent file |
so | SOCK | Socket |
su | SETUID | File that is setuid (u+s) |
sg | SETGID | File that is setgid (g+s) |
tw | STICKY_OTHER_WRITABLE | Directory that is sticky and other-writable (+t,o+w) |
ow | OTHER_WRITABLE | Directory that is other-writable (o+w) and not sticky |
st | STICKY | Directory with the sticky bit set (+t) and not other-writable |
ex | EXEC | Executable file (i.e. has 'x' set in permissions) |
mi | MISSING | Non-existent or missing file pointed to by a symbolic link (visible when you type ls -l) |
lc | LEFTCODE, LEFT | Opening terminal code |
rc | RIGHTCODE, RIGHT | Closing terminal code |
ec | ENDCODE, END | Non-filename text |
*.extension | FILE TYPE CODE | Any file with any extension |
Effects
Code | Property |
00 | Default color |
01 | Bold font |
04 | Underlined |
05 | Flashing text |
07 | Reverse |
08 | Concealed |
Colors
Code | Property |
30 | Black |
31 | Red |
32 | Green |
33 | Orange |
34 | Blue |
35 | Purple |
36 | Cyan |
37 | Grey |
Background Colors
Code | Property |
40 | Black background |
41 | Red background |
42 | Green background |
43 | Orange background |
44 | Blue background |
45 | Purple background |
46 | Cyan background |
47 | Grey background |
Additional Colors
Code | Property |
90 | Dark grey |
91 | Light red |
92 | Light green |
93 | Yellow |
94 | Light blue |
95 | Light purple |
96 | Turquoise |
97 | White |
100 | Dark grey background |
101 | Light red background |
102 | Light green background |
103 | Yellow background |
104 | Light blue background |
105 | Light purple background |
106 | Turquoise background |
107 | White 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
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.
Comments