Friday, July 24, 2009

ls colors - green only if the file has exec permission

In my system, all the .sh are displayed in green color when I use the ls command. It is displayed in green color even if the execute bit is not set in the permissions. It confused me because by seeing the green color, I think that the file is executable. When I try to run it with ./.sh it says 'Permission denied'. Only then I will know that the .sh file doesn't have execute permission. So I wanted the files to be displayed in green only if the execute permission is set.

In linux the command 'dircolors' prints out the commands to set the LS_COLORS environment variable. But it doesn't print out what is set in your LS_COLORS variable. Without knowing this, I was first confused why dircolors and 'echo $LS_COLORS' prints out different things. In the LS_COLORS there was an entry for .sh files (*.sh=00;32). This is reponsible for ls to print all the sh files in green color. From where did the LS_COLORS get this entry? I searched in the /etc/DIR_COLORS file. It had an entry, but even after commenting out that LS_COLORS still had that entry in my system.

While trying to find out what is initializing the LS_COLORS with that entry, I came across this page
Wednesday Why: Colour ls that clearly explains how the LS_COLORS variable is initialized.

This script, which is also responsible for setting up the ls alias, gets the colors from one of several possible configuration files; these are from highest to lowest priority: ~/.dir_colors.$TERM, ~/.dir_colors, ~/.dircolors.$TERM, ~/.dircolors, /etc/DIR_COLORS.$TERM, or /etc/DIR_COLORS (where $TERM is your current terminal type as specified by the TERM environment variable).


In my system, /etc/DIR_COLORS.xterm had this line (".sh 00;32"). After commenting that line I got what I expected ie. sh files to be displayed in green only if it has executable permission. I had to the same thing for .bat, .csh also.

No comments:

Post a Comment