How to enable colors for ls command output

In this article, I will write an article about setting colors for ls command output in a bash shell. In Linux, ls is one of the basic and most frequently used commands for listing directory contents in our daily management tasks.

If you have noticed that the ls command displays different colors for directories and files, then we will discuss in this article how to set and change the desired colors for the ls command.

Default ls color

If you are using any Linux operating system (such as Ubuntu or CentOS), the default GUI terminal and console shell will display files in various contextual colors. However, if a computer that does not support color catalog listings by default uses the following command, you may have to add colors to the ls command: ls --color.

Run the following command to display the default colors of any directories and files on the system.

$ ls
$ ls -al /tmp
$ ls -al --color /tmp

ls output

You can also use the following command to check how the colors are assigned.

$ dircolors
$ echo $LS_COLOR

dircolors

Here, you can see a long list of digital codes for file types and default color schemes, and we will explain how to create them yourself.

The color output by the ls command is defined as an alias in bash. Run the following combination of alias and grep command to find the bash shell alias for color display.

$ alias | grep ls

Now, run the following command to delete the alias using the unalias command.

$ unalias ls

Or just run the following Please close colour.

$ ls --color=none

Turn off ls color

You can see in the screenshot that after deleting the alias, the output of the ls command is monochrome.

Now to Enable color, Please use the following command to assign an alias.

$ alias ls='ls --color=auto'

Turn on ls color

You can see in the screenshot that after deleting the alias, the output of the ls command is monochrome.

Now, to enable the color, use the following command to assign an alias.

$ alias ls='ls --color=auto'

Default color code scheme

In the process of assembling the file type and color list, we need to specify any number in the form of filetype = color, separated by a colon (:).

The following is a list of color schemes set by default.

●Colorless (white): File or non-file name text (for example, permissions in the output of ls -l) ●Blue bold: directory ●Blue bold cyan: symbolic link ●Green bold: executable file ●Red Bold: archive files ● Red magenta bold: image files, videos, graphics, etc. OR door or socket ● Cyan: audio files ● Black background: pipe (AKA FIFO) ● Black background bold: block device or character device ● Bold with black background: isolated symbolic links or missing files ● Colorless, with red background: set-user-ID file ● Black, with background: set-group-ID file ● Black, with background: file with function ● White with blue background: sticky directory ●Blue with green background: other writable directories ●black and green background: sticky and other writable directories

To assemble your own list, we need to know the list of color codes and file type codes, which use the same numeric color codes as in the Bash prompt.

You can find below the list of color codes for foreground text:

● Black: 30 ●: 31 ● Green: 32 ●: 33 ● Blue: 34 ● Purple: 35 ● Cyan: 36 ● White: 37

Change custom ls color

In order to set a custom color ls command, You can also update the alias to ~/.bash_profile Either ~/.bashrc Edit the file by editing the file with any text editor vim.

Before making any changes to bashrc, please back up the file by copying its configuration to any other file.

$ cp .bashrc .bashrc2

If you make a mistake or have trouble, you can replace the .bashrc file by typing:

$ cp .bashrc2 .bashrc

Now, open the bashrc file using vim command.

$ vim .bashrc

.bashrc

## Colorize the ls output ##
alias ls='ls --color=auto'

## Use a long listing format ##
alias ll='ls -la'

## Show hidden files ##
alias l.='ls -d .* --color=auto'

bashrc ls color settings

After making any changes to the baschrc file, next save the file and run the following command to update the bash changes.

$ source .bashrc

For example, see how it works in the following steps. If you want to change the color of the directory from the default bold blue to bold, please run the following command in the terminal as shown in the figure.

$ LS_COLORS=”di=1;33”

ls example changes color to yellow

You can add new key-value pairs at the end LS_COLORS environment variable Or just edit the value of a specific key.

For example, di = 0; 33, where di means that the color only affects the catalog, 0 means normal color, and 33 means normal color.

If you want to keep the bold font for the catalog, the color code should be di = 1; 33, and 1 means the bold font.

More examples

Let’s use the following commands to have fun Change color Folders and some specific file extensions.

$ export LS_COLORS=$LS_COLORS:"*.txt=01;36":"*.mp3=01;31"

Change ls color

Here, you can see that we have updated the color of the .txt file to Cyan and the file color of the .mp3 extension to.

Let’s run another command below to set the color of the directory to “purple”.

$ export LS_COLORS="di=0;35"

Change ls output color to purple

Once you are familiar with setting custom colors, you can add these changes to the dot bashrc file located in the user’s home directory to make these changes permanent.

in conclusion

At the end of this tutorial, you should be familiar with the colors of the ls command, what they mean and how we can enable or disable them and change them for convenience. Setting LS_COLORS can make the ls list look more beautiful, which helps you identify files when traversing the file system.

Sidebar