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:
dircolors echo $LS_COLOR
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
|no||NORMAL, NORM||Global default|
|ln||SYMLINK, LINK, LNK||Symbolic link|
|pi||FIFO, PIPE||Named pipe|
|bd||BLOCK, BLK||Block device|
|cd||CHAR, CHR||Character device|
|or||ORPHAN||Symbolic link pointing to a non-existent file|
|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|
|100||Dark grey background|
|101||Light red background|
|102||Light green background|
|104||Light blue background|
|105||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.