How to Enable Colors for ls Command Output

set colors for ls command

In this article, I am going to write about setting up colors for ls command output in your bash shell. In Linux, ls is one of the basic and most repeatedly used commands in our daily administration tasks to list the contents of a directory.

If you have noticed ls command shows different colors for the directories and files, that's what we are going to discuss here in this article to set and change desired colors for the ls command.

Default ls Colors

If you are using any Linux OS like Ubuntu or CentOS, the default GUI terminals and console shell prompt display files in various contextual colors. However, you may have to add color to the ls command if a machine that does not support colored directory listings by default using ls --color.

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

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

ls output

You can also make use of the following commands to check how the colors are assigned.

$ dircolors
$ echo $LS_COLOR

dircolors

Here you can see a long list of file types and number codes of default coloring scheme that we will explain how you can create by yourself.

Turning ON/OFF colors for ls command

The colors of ls command output is defined in bash as aliases. Run the combination of following aliases and grep command to find the bash shell aliases used for color display.

$ alias | grep ls

Now run the commands below to remove alias with the unalias command.

$ unalias ls

Or just run the following ls to turn off the colors.

$ ls --color=none

turn off ls colors

You can see in the screenshot that after removing the alias the output of ls command is uni color.

Now, to enable the colors, assign back the alias using the following command.

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

turn on ls colors

You can see in the screenshot that after removing the alias the output of ls command is uni color.

Now, to enable the colors, assign back the alias using the following command.

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

Default Colors Code Scheme

In the process for assembling your list of file types and colors, we need to specify as many as you like in the form filetype=color, separating each with a colon (:) character.

Following is the list of coloring schemes with default setup.

● Uncolored (white): file or non-filename text (e.g. permissions in the output of ls -l)
● Bold blue: directory
● Bold cyan: symbolic link
● Bold green: executable file
● Bold red: archive file
● Bold magenta: image file, video, graphic, etc. or door or socket
● Cyan: audio file
● with black background: pipe (AKA FIFO)
● Bold with black background: block device or character device
● Bold with black background: orphan symlink or missing file
● Uncolored with red background: set-user-ID file
● Black with background: set-group-ID file
● Black with background: file with capability
● White with blue background: sticky directory
● Blue with green background: other-writable directory
● Black with green background: sticky and other-writable directory

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

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

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

Change Custom ls colors

In order to setup custom colors for the ls command, this can also be done through updating the aliases to the ~/.bash_profile or ~/.bashrc file by editing the file using any text editor like vim.

Before making any changes to the bashrc, first take the backup of this file by copying its configurations to any other file.

$ cp .bashrc .bashrc2

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

$ cp .bashrc2 .bashrc

Now let's open up the bashrc file using the 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 setup

Once you have made any changes to the baschrc file, next save it and run the command below to update your bash changes.

$ source .bashrc

Let’s for example see how this works in the following steps. When you want to change the color of directories from the default bold blue to bold, run the following commands in your terminal as shown.

$ LS_COLORS=”di=1;33”

ls example change color to yellow

You can add a new key value pair at the end of the LS_COLORS environment variable or simply Edit the value of a specific key.

For example, di=0;33, here di means the color will impact only to directories whereas 0 represents it’s a normal color, and 33 represents the color is.

If you wanted to keep a bold font for the directories, the color code should be di=1;33 whereas 1 represents bold font.

More Examples

Let’s play around by using the following commands to change the colors of your 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 for .txt files to Cyan and for the files with .mp3 extension to .

Let's run another command below to set the color of your directories to Purple.

$ export LS_COLORS="di=0;35"

change ls output color to purple

Once you are familiar with setting up custom colors, you can make these changes permanent by adding them to your dot bashrc files located in your users home directory.

Conclusion

At the end of this tutorial, you should be familiar with the colors of ls command, what they represent and how we can enable or disable and change them for our own convenience. Setting your LS_COLORS makes your ls listings look more pretty which helps you in identifying files while wading through a file system.

Kashif Siddique 4:10 am

Comments

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