What causes this green background in ls output?
There are two directories shown by ‘ls’. Normally directories anywhere are blue on black background. But the first one is blue on green and impossible to read. Why is this? How to make it blue on black, or at least something light on something dark?
This is on Ubuntu 12.04, using bash in Gnome Terminal. In Konsole, the blue is slightly darker, and possible to read, though could be way better.
Apart from coloring files based on their type (turquoise for audio files, bright red for Archives and compressed files, and purple for images and videos), ls
also colors files and directories based on their attributes:
- Black text with green background indicates that a directory is writable by others apart from the owning user and group, and has the sticky bit set (
o+w, +t
). - Blue text with green background indicates that a directory is writable by others apart from the owning user and group, and does not have the sticky bit set (
o+w, -t
).
Stephano Palazzo over at Ask Ubuntu has made this very instructive picture over the different attribute colors:
As terdon pointed out, the color settings can be modified via dircolors
. A list of the different coloring settings can be accessed with dircolors --print-database
.
Each line of output, such as BLK 40;33;01
, is of the form:
[TARGET] [TEXT_STYLE];[FOREGROUND_COLOR];[BACKGROUND_COLOR]
-
TARGET
indicates the target for the coloring rule -
TEXT_STYLE
indicates the text style:00
= none01
= bold04
= underscore05
= blink07
= reverse,08
= concealed
-
FOREGROUND_COLOR
indicates the foreground color:30
= black31
= red32
= green33
= yellow34
= blue,35
= magenta36
= cyan37
= white
-
BACKGROUND_COLOR
indicates the background colors:40
= black41
= red42
= green43
= yellow44
= blue,45
= magenta46
= cyan47
= white
Fields may be omitted starting from the right, so for instance .tar 01;31
means bold and red.
XTerm and most other modern terminal emulators support 256 colors.
A XTerm 256-color foreground color code is of the form:
38;5;[FOREGROUND_COLOR]
A XTerm 256-color background color code is of the form:
48;5;[BACKGROUND_COLOR]
where both FOREGROUND_COLOR
and BACKGROUND_COLOR
is a number the range 0-255. A full list of color codes for the 16 and 256 color modes are shown in the below screenshot:
The colors of ls
can represent the permissions; the defaults for some systems is to show directories where everyone has write permissions with a green background:
You can change the colors by editing your $LS_COLORS
variable using dircolors
(from man ls
):
Using color to distinguish file types is disabled both by default and
with --color=never. With --color=auto, ls emits color codes only when
standard output is connected to a terminal. The LS_COLORS environment
variable can change the settings. Use the dircolors command to set it.
The syntax is admittedly kind of annoying here but you can change this color by creating a file with the colors you want and saving it as ~/.dircolors
:
dircolors -p > ~/.dircolors
That command will print the defaults into ~/.dircolors
. You will then need to edit that file and change this line:
OTHER_WRITABLE 34;42 # dir that is other-writable (o+w) and not sticky
For example, to make it black text on a red background (see here for a list of color codes):
OTHER_WRITABLE 30;41 # dir that is other-writable (o+w) and not sticky
You don’t need to have all the defaults, you can also just create a file with a single line, redefining just the one you want to change. Anyway, once you have created the file, load it with:
eval "$(dircolors ~/.dircolors)";
And here it is in action:
To have that happen automatically, add the eval
command above to your ~/.bashrc
file.
Here are the 3 steps I used to change the colors:
First, copy the default colors to a file
dircolors -p > ~/.dircolors
Then modify this file. You can find some values for colors inside, and here are some more:
Code Color
0 Default Colour
1 Bold
4 Underlined
5 Flashing Text
7 Reverse Field
31 Red
32 Green
33 Orange
34 Blue
35 Purple
36 Cyan
37 Grey
40 Black Background
41 Red Background
42 Green Background
43 Orange Background
44 Blue Background
45 Purple Background
46 Cyan Background
47 Grey Background
90 Dark Grey
91 Light Red
92 Light Green
93 Yellow
94 Light Blue
95 Light Purple
96 Turquoise
100 Dark Grey Background
101 Light Red Background
102 Light Green Background
103 Yellow Background
104 Light Blue Background
105 Light Purple Background
106 Turquoise Background
(source)
And finally, add the following line to your ~/.bashrc
file for the colors to be automatically loaded when you open a terminal:
eval 'dircolors ~/.dircolors' > /dev/null
For ~/.zshrc
:
if [[ -f ~/.dircolors ]] ; then
eval $(dircolors -b ~/.dircolors)
elif [[ -f /etc/DIR_COLORS ]] ; then
eval $(dircolors -b /etc/DIR_COLORS)
fi
While all the technical answers are true, I would consider it a bit of an informal warning, that you dished out rights a bit to generously or copying criss+cross… (most often we all do, to get things initially working, eh?)
How to make it blue on black,… ?
A good „de-greener“ to get back to rights you most likely want, is this statement:
chmod -R a-x,o-w,+X thatGreenFolderWithSubfolders/
Best understood what it does, if you understand the purpose of uppercase +X „special execute“, i.e. see Wikipedia
It is only really useful when used with ‘+’
and usually in combination with the -R option for giving group or
other access to a big directory tree without setting execute
permission on normal files (such as text files), which would normally
happen if you just used “chmod -R a+rx…
To fix this try the ow parameter to the LS_COLORS
For example:
~LS_COLORS='fi=0:ln=5:pi=0:so=7:bd=5:cd=5:or=31:mi=0:ex=93:*.py=36:di=40:*.zip=33:*.tgz=33'
~ls -l
Now you add the ow (OTHER_WRITABLE
) option
~export LS_COLORS='fi=0:ln=5:pi=0:so=7:bd=5:cd=5:or=31:mi=0:ex=93:*.py=36:di=40:*.zip=33:*.tgz=33:ow=0'
~ls -l
Well this mean that this folder has permissions if you run this:
chmod og-w AU_LI
it will remove bgcolor 🙂
You can change the tone of green in Putty to make the text readable.
Open Putty and go to WindowColours,
select “ANSI Green”,
set it to a darker green(R:0 G:70 B:0).
tldr; How to just fix it?
To quickly fix the problem:
LS_COLORS+=':ow=01;33'
- Makes other-writable files show up as yellow on nobg
- Edit your shell profile (e.g.
~/.bashrc
,~/.profile
, etc) to make this permanent.
More details:
Replace 33
by 34
for blue
on nobg
. Even simpler, to make it nofg
on nobg
:
LS_COLORS+=:ow=
To make your change permanent, append it to your .profile:
echo "export LS_COLORS+=':ow=01;33'" >> ~/.profile
To view the non-extension related rules of LS_COLORS
:
echo "$LS_COLORS" | sed 's/:/n/g' | grep -v '*.'
sed
puts each rule on one line and grep
removes the rules beginning by *.'.
To explore the ls
colors on your terminal, consider using
C="$LS_COLORS"
function sc () {
echo "$LS_COLORS" | sed 's/:/n/g' | grep -v '*.'
}
function t () {
ls /mnt # Or the path to your example directory.
}
Then
LS_COLORS="$C:ow=38;5;250;48;5;025";t
As stated in another answer (that of Thomas Nyman), 38;5;
is the prefix for foreground x-term 256-colors, and 48;5;
for background x-term 256-colors. 256-colors isn’t supported by all terminals though.
Also see -What do the different colors mean in ls?- on AskUbuntu.
Make all directories the same normal color. Copy/paste this to the end of the file ~/.bashrc
export LS_COLORS+=':tw=01;34:ow=01;34:st=01;34'
Start a new terminal to see the changes
In git-bash / MINGW the folder colors is often an issue for me. I like to use the following command as it makes things obvious:
eval "$(dircolors <(dircolors -p | grep -v '^[.#]' | sed -E '/DIR/s/[0-9;]{5}/01;33/g'))"