How to change the colors of the ls command in Linux

El ls command It is one of the most used when we work in the console, we note that we have not dedicated any article to this excellent command on the blog, so we bring the tutorial of how to change ls command colors. In the same way, we are going to give a brief explanation of this command and its use.

We are going to base ourselves on official information and various methods found on the web for the same purpose, so this article is going to be a compilation of various proven and working methods performed by various individuals. change the colors of the ls command

What is the ls command?

Quoting Wikipedia:

«ls (of English list, whose translation is list, list or list) is a Linux command and derivatives that shows a list of files and directories in a certain directory. Results are listed alphabetically.

Files and directories whose name begins with . (period) are not displayed with the instruction ls, which is why they are often called "hidden files." The option -a de ls inhibits this behavior, and shows all files and subdirectories, even those that start with a period.

ls it is one of the most basic tools of operating systems Unix, so it is part of the package GNU Coreutils.»

Default colors in ls command results

By default, each of the colors that it throws when we execute the ls command have a meaning, since it is the way to classify files according to their characteristics.

  • Verde: Executable files.
  • Black: Normal file.
  • BLUE: Directories or folders.
  • Celeste's: Symbolic link.
  • RED: Compressed files (.tar, .gz, .zip, .rpm).
  • Magenta: Image files (.jpg, gif, bmp, png, tif)

How to change the colors of the ls command

Option 1: Modifying our .bashrc

For the results of the ls command to be of a different color from the default, we must modify our .bashrc file, for this we must perform the following steps:

Edit our .bashrc file from the terminal

nano $HOME/.bashrc

Add the following to the end of the file:

export PS1="\[$(tput setaf 1)\]\u@\h:\w $ \[$(tput sgr0)\]"

Upload the file and enjoy.

source ~/.bashrc

You can edit the export using the following settings.

List of options:

  • tput bold - bold
  • tput rev - inverted colors
  • tput sgr0 - Reset all
  • tput setaf {CODE} - Set foreground color, see color {CODE}

COLOR CODE:

Color {code}    Color
0   Black
1   Red
2   Green
3   Yellow
4   Blue
5   Magenta
6   Cyan
7   White

Option 2: Another way to modify our .bashrc

As we did in the previous step, we must modify our .bashrc file, for this we must perform the following steps:

Edit our .bashrc file from the terminal

nano $HOME/.bashrc

Add the following to the end of the file:

alias ls='ls --color
LS_COLORS='di=1:fi=0:ln=31:pi=5:so=5:bd=5:cd=5:or=31:mi=0:ex=35:*.rpm=90'
export LS_COLORS

The first line makes ls use the parameter -Colour by default, which tells ls that shows its outputs based on the setting of the variable.

The second line represents the color you want to give to the various linux files, they are represented by the following denotation:

di = directory
fi = file
ln = symbolic link
pi = FIFO file
so = socket file
bd = block (buffer) of special files
cd = character (unbuffered) from special files
or = symbolic link pointing to a non-existent file (orphan)
mi = non-existent file pointed to by a symbolic link (visible when typing ls -l)
ex = executable file

The colors that accompany each type of file are represented by numbers, to know the conversion of each color you can see the following table:

0 = default color
1 = bold
4 = underlined
5 = flashing text
7 = reverse field
31 = red
32 = green
33 = orange
34 = blue 
35 = purple
36 = magenta 
37 = gray
40 = black background
41 = red background
42 = green background 
43 = orange background
44 = blue background
45 = purple background
46 = cyan background
47 = gray background
90 = dark gray
91 = red light
92 = green light
93 = yellow
94 = blue light
95 = violet light
96 = turquoise
100 = gray background
101 = red background 
102 = light green background
103 = yellow background
104 = light blue background
105 = purple backlight
106 = turquoise background

Option 3: Using LS_COLORS

Another way to change colors is using LS_COLORS, a collection of colors that allows us to assign colors to the output of the ls command. To use it, just enter the terminal and perform the following commands:

wget https://raw.github.com/trapd00r/LS_COLORS/master/LS_COLORS -O $HOME/.dircolors
echo 'eval $(dircolors -b $HOME/.dircolors)' >> $HOME/.bashrc
. $HOME/.bashrc

With these various forms of change the colors of the ls command, you can configure the representation of the output of this great command to your liking.

With information stackoverflow y linux-sxs


Leave a Comment

Your email address will not be published. Required fields are marked with *

*

*

  1. Responsible for the data: Miguel Ángel Gatón
  2. Purpose of the data: Control SPAM, comment management.
  3. Legitimation: Your consent
  4. Communication of the data: The data will not be communicated to third parties except by legal obligation.
  5. Data storage: Database hosted by Occentus Networks (EU)
  6. Rights: At any time you can limit, recover and delete your information.

  1.   Gonzalo Martinez said

    Since you quote Wikipedia leave it unchanged.

    "Ls" is a UNIX and derivatives command, not a Linux and derivatives command.

    Not everything is an invention of Linux and free software.