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:
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:
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'.
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:
The color code syntax consists of four parts. Take the following example for a better understanding:
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
|SYMLINK, LINK, LNK
|Symbolic link pointing to a non-existent file
|File that is setuid (u+s)
|File that is setgid (g+s)
|Directory that is sticky and other-writable (+t,o+w)
|Directory that is other-writable (o+w) and not sticky
|Directory with the sticky bit set (+t) and not other-writable
|Executable file (i.e. has 'x' set in permissions)
|Non-existent or missing file pointed to by a symbolic link (visible when you type ls -l)
|Opening terminal code
|Closing terminal code
|FILE TYPE CODE
|Any file with any extension
|Dark grey background
|Light red background
|Light green background
|Light blue background
|Light purple 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.
For example, affix the following value to set different colors for files with different extensions:
Then save your .bashrc file and source it.
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
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.