File color in linux

How to Enable Colors for ls Command Output

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.

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

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.

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

Or just run the following ls to turn off the 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.

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.

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

Читайте также:  Курсы администратор windows server

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

/.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.

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

Now let’s open up the bashrc file using the vim command.

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

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.

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.

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.

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.

Читайте также:  Windows games installer download

Источник

Understanding the Color Code of Linux files

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.

The default color code configuration file is located in /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 .

If you want to make it permanent, just append the value into the “.bashrc” file :

Finally, run the below command for the changes to take effect:

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.

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.

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!

Источник

Understanding the Color Code of Linux Files

In Linux everything present is considered as a file, so to differentiate file types from each other for better visibility to user, Linux has specific color codes for the different file types.

Below, we will try to create some of the files and know its colors.

White Color – Indicates regular files. so it can be text file, word document or pdf document etc. Lets create simple text file as,

All the normal files created / present, by default has the permission set to “664”

Bright Green – Indicates file which are executable. All the executable files in Linux has “x” i.e. executable permissions set, which makes the permissions as “775”
Lets create one simple C program and compile it to generate executable as,

Compile to generate executable file as,

We can check the default permissions set by above compilation for executable “helloworld_exe” is “775” as,

Blue – Indicates its a directory. Default permissions of the newly created directory is 775

Yellow – Indicates its a device file. Most of the device files created by Linux kernel resides in /dev . Below is an example of device file which will be displayed in yellow color.

Cyan / greenish-blue – Indicates its a softlink to another file. We can create a softlink to our previously created executable as,

Red with light black background – Indicates its a broken softlink. Means, the file where the link was pointing has been deleted.
We will try to create a broken softlink as,

Here, helloworld_exe1 we purposefully deleted to create broken softlink.

Magneta ( a light mauvish-crimson color ) Indicates it is a “Image” or “Video” file depending on the extension of file.


Red – Indicates its archive / compressed file.

We will create a simple zip with the existing files as,

So finally this will Look Like below,

Источник

Читайте также:  Windows server dfs replication
Оцените статью