I'm sure you must have used the ls command to list the contents of a directory. In Ubuntu and many other distributions, you'll see the ls command output in different colors.
If you don't see it, you can get colored output like this:
ls --color=auto
But have you ever wondered what those colors mean in the ls command output?
I'll answer the questions in this article. I'll also show how to get colored output with ls command if your terminal does not show it by default.
Meaning of colors used in the ls command in Ubuntu
I am using the default Ubuntu terminal and its color profile in this section.
When you list files using the ls command, it will use different colors to indicate different kinds of files.
Most terminals will show the executable files, links, regular files and directories in different colors so that you can easily distinguish between them.
Some terminals, like the default one in Ubuntu, take it to the next level and add more colors for music files, images and videos.
For the demonstrations, I've listed files from different directories which filled my terminal window with different colors in Ubuntu:
Looks confusing? Let me decode each one for you!
Color | Description |
---|---|
Bold Blue | Directories. |
Uncolored | File or multi-hard link. |
Bold Cyan | A symbolic link pointing to a file. |
Bold Green | An executable file (scripts with having an .sh extension). |
Bold Red | Archive file (mostly a tarball or zip file). |
Magenta | Indicates images and video files. |
Cyan | Audio files. |
Yellow with black bg | A pipe file (known as FIFO). |
Blod red with black bg | A broken symbolic link. |
Uncolored (white) with red bg | Indicates set-user-ID file. |
Black with yellow bg | Indicates set-group-ID file. |
White with blue bg | Shows a sticky directory. |
Blue with green bg | Points to Other-writable directory |
Black with green bg | When a directory has characteristics of both sticky and other-writable directories. |
But what if your terminal doesn't show any colors? Well, there's a reason and solution for that.
What if the ls command does not show colored output?
Here's the thing. The ls command is not supposed to display colors in output by default. It will show the colors if you use the --color=auto
flag.
ls --color=auto
Then why does the ls command add colors by default in Ubuntu and some other distributions? That's because your distribution has an alias set for the ls command to use the --color=auto
flag when you execute the ls command:
alias
So if the ls command is not showing the colorful output, the alias is not set by default.
Now, you may use --color=auto
flag whenever you use the ls command
But that's not very convenient. Instead, you should create alias and add it to your bashrc so that ls command displays colors by default.
Create a permanent alias for ls to display colors
To create a permanent alias, first, open the .bashrc
file using the following command:
nano ~/.bashrc
Go to the end of the file using Alt + /
and paste the following line in the terminal:
alias ls='ls --color=auto'
Once done, save changes and exit from the nano text editor.
To take effect from the changes you've just made, source the .bashrc
file:
source ~/.bashrc
That's it! From now on, you can use the colored output.
Where does the ls command gets the color from?
Now comes the interesting part. Where are the colors for the ls command defined? The answer is LS_COLORS.
Yes. That's the special environment variable called LS_COLORS that defines the colors used by the ls command.
That's good but who defines this variable? What if you wanted to make some changes? I'll answer these questions as well.
Actually, you have a dedicated dircolors
command to setup color for the ls command.
Different shells have different formats for the color profile. This is why you should indicate the shell when you use this command.
As you can see, it defines the LS_COLORS environment variable and exports it so that the variable is available for the subshell.
Now, if you want to use it, you can either copy-paste it to your bashrc file or redirect the output like this:
dircolors -b >> .bashrc
And source the file so that the effects are immediately visible. You have to do it only once.
Understanding the color profile
The LS_COLORS has data in key-value pair separated by a colon (:). If the value has more than one part, they are separated by a semicolon (;).
The key is usually predefined. The value part represents the colors.
So, if it says ln=01;36
, it means for symbolic links, the font is bold and the color (36) is cyan.
0 is for normal, 1 is for bold, 4 is for underlined. 31 is for red, 32 is for green etc. The color codes follow the ANSI escape code.
Another example. or=40;31;01
means that link to a non-existent file (key is or) uses black background (color code 40), red color and bold font (code 01).
I think the order doesn't matter because the codes don't overlap. 31 is the code for the foreground red color and 41 is the color for the background red color. So if 41 is used, you know it is for the background color.
Do more with the ls command
The ls command can do a lot more and for that purpose, we made a detailed tutorial on how to use the ls command:
This tutorial was requested by an It's FOSS member in our community forum. If you have suggestions or tutorial requests, please use our Community platform.
With inputs from Abhishek Prakash.
0 Commentaires