Everything in Linux is considered a file (including a hard disk, graphics card, USB, etc,.). Hence, Linux has specific color codes for different file types to distinguish them from each other for better viewing by the user.
As a Linux administrator you don’t have to memorize all the color code’s, but you do need to remember some color codes that you use on a daily basis.
Typically, you will see different colors for the directory, link file and archive file when using the “ls” command on your terminal.
The ls command uses the environment variable “LS_COLORS” to determine the colors in which the filenames are to be displayed.
You can see the list of file types and their respective color codes by calling the LS_COLORS
variable. You may not understand anything when you first see this output, but we will explain it in detail here.
# echo $LS_COLORS rs=0:di=01;34:ln=01;36:mh=00:pi=40;33:so=01;35:do=01;35:bd=40;33;01:cd=40;33;01:or=40;31;01:mi=01;05;37;41:su=37;41:sg=30;43:ca=30;41:tw=30;42:ow=34;42:st=37;44:ex=01;32:*.tar=01;31:*.tgz=01;31:*.arc=01;31:*.arj=01;31:*.taz=01;31:*.lha=01;31:*.lz4=01;31:*.lzh=01;31:*.lzma=01;31:*.tlz=01;31:*.txz=01;31:*.tzo=01;31:*.t7z=01;31:*.zip=01;31:*.z=01;31:*.Z=01;31:*.dz=01;31:*.gz=01;31:*.lrz=01;31:*.lz=01;31:*.lzo=01;31:*.xz=01;31:*.bz2=01;31:*.bz=01;31:*.tbz=01;31:*.tbz2=01;31:*.tz=01;31:*.deb=01;31:*.rpm=01;31:*.jar=01;31:*.war=01;31:*.ear=01;31:*.sar=01;31:*.rar=01;31:*.alz=01;31:*.ace=01;31:*.zoo=01;31:*.cpio=01;31:*.7z=01;31:*.rz=01;31:*.cab=01;31:*.jpg=01;35:*.jpeg=01;35:*.gif=01;35:*.bmp=01;35:*.pbm=01;35:*.pgm=01;35:*.ppm=01;35:*.tga=01;35:*.xbm=01;35:*.xpm=01;35:*.tif=01;35:*.tiff=01;35:*.png=01;35:*.svg=01;35:*.svgz=01;35:*.mng=01;35:*.pcx=01;35:*.mov=01;35:*.mpg=01;35:*.mpeg=01;35:*.m2v=01;35:*.mkv=01;35:*.webm=01;35:*.ogm=01;35:*.mp4=01;35:*.m4v=01;35:*.mp4v=01;35:*.vob=01;35:*.qt=01;35:*.nuv=01;35:*.wmv=01;35:*.asf=01;35:*.rm=01;35:*.rmvb=01;35:*.flc=01;35:*.avi=01;35:*.fli=01;35:*.flv=01;35:*.gl=01;35:*.dl=01;35:*.xcf=01;35:*.xwd=01;35:*.yuv=01;35:*.cgm=01;35:*.emf=01;35:*.axv=01;35:*.anx=01;35:*.ogv=01;35:*.ogx=01;35:*.aac=01;36:*.au=01;36:*.flac=01;36:*.mid=01;36:*.midi=01;36:*.mka=01;36:*.mp3=01;36:*.mpc=01;36:*.ogg=01;36:*.ra=01;36:*.wav=01;36:*.axa=01;36:*.oga=01;36:*.spx=01;36:*.xspf=01;36:
The default color code configuration file is located in /etc/DIR_COLORS
.
# cat /etc/DIR_COLORS
It uses three types of color codes for files:
- File attribute codes
- Text color codes
- Background color codes
There are many color codes, but you will often see only the 7 colors listed below:
- White (No color code): Regular File or Normal File
- Blue: Directory
- Bright Green: Executable File
- Bright Red: Archive file or Compressed File
- Magenta: Image File
- Cyan: Audio File
- Sky Blue: Symbolic Link File
List of file type codes
List of file type codes and symbols are listed below. The “ls” command will help you identify and categorize the seven different file types found on a Linux system. File type symbols appear as the first character of the file permission part, which is used to identify the file type.
Code | symbol | File Types |
---|---|---|
di | d | Directory |
fi | – | File |
ex | – | Executable File |
ln | l | Symbolic Link File |
pi | p | Named Pipe (FIFO) |
so | s | Socket |
bd | b | Block Device |
cd | c | Character Device |
mi | Missing File | |
*.extension | – | Example: *.mp3, *.png, etc,. |
List of text code attributes
Here is the list of text code attributes.
Code | Attributes |
---|---|
00 | None |
01 | Bold |
04 | Underscore |
05 | Blink |
07 | Reverse |
08 | Concealed |
List of text & background color codes
Here is the list of Text color codes:
Color | Foreground Code (Text) | Background Code |
---|---|---|
Black | 30 | 40 |
Red | 31 | 41 |
Green | 32 | 42 |
Yellow | 33 | 43 |
Blue | 34 | 44 |
Magenta | 35 | 45 |
Cyan | 36 | 46 |
White | 37 | 47 |
dark grey | 90 | 100 |
light red | 91 | 101 |
light green | 92 | 102 |
light yellow | 93 | 103 |
light blue | 94 | 104 |
light purple | 95 | 105 |
turquoise | 96 | 106 |
light white | 97 | 107 |
How to set custom Color for files in Linux
You can change the default color code of Linux files if you want to try a different color instead.
To do so, follow the below procedure. The default folder color is “Blue”, but we are going to change that to “Yellow” with “Underline”.
To do so, the combination should be di=4;33
.
# LS_COLORS="di=4;33" # ls -lh
If you want to make it permanent, just append the value into the “.bashrc” file :
$ echo "LS_COLORS="di=4;33"" >> ~/.bashrc
Finally, run the below command for the changes to take effect:
$ source ~/.bashrc
Changing the text color
By default any command output displays in “White” color on the terminal as well as in the shell script. You can change the text color of the command output as per your choice by using the color code. For instance, I will be printing the following text with “Green” color.
$ echo $'\e[1;32m'Welcome to 2DayGeek$'\e[0m'
Alternatively, you can use a number of commands for this purpose such as “lolcat”, “tput” and “highlight”. However, we will use “tput” as it comes pre-installed as part of core utility.
Print the following text in “Red” color, print only the “White” background for the last part of the text, using the following command.
$ echo "$(tput setaf 1)$(tput bold)Welcome to $(tput setab 7)2DayGeek$(tput sgr 0)"
Closing Notes
This article explained how to use color codes for files & directories in Linux.
If you found this article helpful, please do share with your friends and spread the knowledge. Please feel free to comment below if you have any queries/concerns. We will get back to you as soon as we can. Happy learning!