167

On my console the color for directories is such a blue, that it is hard to read on a dark background.

How can I change the color definitions for ls?

250

To change your directory colors, open up your ~/.bashrc file with your editor

nano ~/.bashrc

and make the following entry at the end of the file:

LS_COLORS=$LS_COLORS:'di=0;35:' ; export LS_COLORS

Some nice color choices (in this case 0;35 it is purple) are:

Blue = 34
Green = 32
Light Green = 1;32
Cyan = 36
Red = 31
Purple = 35
Brown = 33
Yellow = 1;33
Bold White = 1;37
Light Grey = 0;37
Black = 30
Dark Grey= 1;30

The first number is the style (1=bold), followed by a semicolon, and then the actual number of the color, possible styles (effects) are:

0   = default colour
1   = bold
4   = underlined
5   = flashing text (disabled on some terminals)
7   = reverse field (exchange foreground and background color)
8   = concealed (invisible)

The possible backgrounds:

40  = black background
41  = red background
42  = green background
43  = orange background
44  = blue background
45  = purple background
46  = cyan background
47  = grey background
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
107 = white background

All possible colors:

31  = red
32  = green
33  = orange
34  = blue
35  = purple
36  = cyan
37  = grey
90  = dark grey
91  = light red
92  = light green
93  = yellow
94  = light blue
95  = light purple
96  = turquoise
97  = white

These can even be combined, so that a parameter like:

di=1;4;31;42

in your LS_COLORS variable would make directories appear in bold underlined red text with a green background!

To test all these colors and styles in your terminal, you can use one of:

for i in 00{2..8} {0{3,4,9},10}{0..7}
do echo -e "$i \e[0;${i}mSubdermatoglyphic text\e[00m  \e[1;${i}mSubdermatoglyphic text\e[00m"
done

for i in 00{2..8} {0{3,4,9},10}{0..7}
do for j in 0 1
   do echo -e "$j;$i \e[$j;${i}mSubdermatoglyphic text\e[00m"
   done
done

You can also change other kinds of files when using the ls command by defining each kind with:

bd = (BLOCK, BLK)   Block device (buffered) special file
cd = (CHAR, CHR)    Character device (unbuffered) special file
di = (DIR)  Directory
do = (DOOR) [Door][1]
ex = (EXEC) Executable file (ie. has 'x' set in permissions)
fi = (FILE) Normal file
ln = (SYMLINK, LINK, LNK)   Symbolic link. If you set this to ‘target’ instead of a numerical value, the color is as for the file pointed to.
mi = (MISSING)  Non-existent file pointed to by a symbolic link (visible when you type ls -l)
no = (NORMAL, NORM) Normal (non-filename) text. Global default, although everything should be something
or = (ORPHAN)   Symbolic link pointing to an orphaned non-existent file
ow = (OTHER_WRITABLE)   Directory that is other-writable (o+w) and not sticky
pi = (FIFO, PIPE)   Named pipe (fifo file)
sg = (SETGID)   File that is setgid (g+s)
so = (SOCK) Socket file
st = (STICKY)   Directory with the sticky bit set (+t) and not other-writable
su = (SETUID)   File that is setuid (u+s)
tw = (STICKY_OTHER_WRITABLE)    Directory that is sticky and other-writable (+t,o+w)
*.extension =   Every file using this extension e.g. *.rpm = files with the ending .rpm

A more complete list is available at Bigsoft - Configuring LS_COLORS.

On some distributions, you might also want to change the background color for ow "(OTHER_WRITABLE) whose default is non-readable" for example to non-bold blue text on green background.

You could use for instance LS_COLORS="$LS_COLORS:di=1;33" at the end of your .bashrc file, to get a nice readable bold orange text on black background.

After you alter your .bashrc file, to put the changes in effect you will have to restart your shell or run source ~/.bashrc.

Note: You can combine more commands with a colon, for example

LS_COLORS=$LS_COLORS:'di=1;33:ln=36' ; export LS_COLORS; ls

Source:

  • 3
    This works well, thanks. I personally found it better to set LS_COLORS=$LS_COLORS:'di=1;44:' ; export LS_COLORS which shows the directories in bold white over blue background. – isapir Mar 18 '16 at 18:52
  • 2
    This doesn't seem to work for me. It looks like there is another config file somewhere that overwrites .bashrc - some colors work, others look differently. Folders are green for instance instead of blue, but files look correctly. What could it be? – Nearoo Jul 9 '16 at 8:42
  • 1
    check if you are using bash or sh, and also check your .profile file in your home directory. and global /etc/bash.bashrc – rubo77 Jul 9 '16 at 9:52
  • 5
    You probably want to modify other writable and sticky other writable dirs. Try something more like LS_COLORS=$LS_COLORS:'tw=01;35:ow=01;35:' ; export LS_COLORS – Charles L. May 25 '17 at 19:57
  • 2
    that worked for ls, but how do i change the color of the dir I am in, before the # in the cmd prompt? – Gaia Jan 3 '18 at 6:00
31

Very simple. Add these three lines to ~/.bashrc

$ vi ~/.bashrc
export LS_OPTIONS='--color=auto'
eval "$(dircolors -b)"
alias ls='ls $LS_OPTIONS'

If you want to apply the changes in a running bash session, run:

source ~/.bashrc
  • 4
    This is the only thing that worked for me. – solalito Jan 29 '17 at 7:54
  • It worked for me but I need to run source ~/.bashrc every time I log in. Is there a way to fix this? – Miguel Nov 29 '18 at 11:11
  • @Miguel : what do you get when you type echo $SHELL from the cmd prompt? I bet it is NOT /bin/bash. You can find your active .profile or .login and add source $HOME/.bashrc at the end. Good luck. – shellter Mar 19 at 18:33
4

LS_COLORS

This is a collection of extension:color mappings, suitable to use as your LS COLORS environment variable.

  • 1
    interesting but I can't make this work in ksh. – Herman Toothrot Aug 17 '16 at 12:50
4

Further to Hegazi's answer, you can actually control the directory colour, and a lot of other colours using the dircolors command. You can create a configuration file which is well documented.

You can create a .dircolor file in your home directory as follows:

dircolors -p > ~/.dircolors

Then in your ~/.bashrc file add the lines

eval "`dircolors -b ~/.dircolors`"
alias ls='ls --color=auto'

This will create a $LS_COLORS variable for bash. The -c flag will set if for csh. It also flags the ls command to display in colour.

Edit the value of the DIR attribute as above in the ~/.dircolor file colour to change the colour of the directory (or any of the other included file types for other colours). You can also change the colours of specific files, or define your own.

2

The --color=auto option doesn't work for me using iTerm2 on my Mac. The -G option works though. I put the following alias in my ~/.profile and now directories are colored and have a trailing / appended:

alias ls='ls -F -G'
  • It's because it's BSD utils and not coreutils :) – Meeh Oct 3 at 19:35
0

The is my approach for ls command with commonly used options:

add the following lines into a file called ~/.alias

alias ll='ls -lhF --color=auto'
alias llt='ls -lht --color=auto'
alias lla='ls -alF --color=auto'
alias la='ls -A --color=auto'
alias l='ls -CF --color=auto'
alias ls="ls --color=auto"

add the following lines into ~/.bashrc

if [ -f ~/.alias ]; then
    . ~/.alias
fi
  • 4
    you only need to add --color=auto to the ls alias - the other aliases with inherit the setting – Stuart Cardall Oct 25 '17 at 16:22

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.