9

How can you make a bash shell list executable files in a different color than non-executable files?

I've tried editing ~/.bashrc to contain the following line (it's otherwise empty):

 LS_OPTIONS='--color=auto'

But it's not working. What am I doing wrong? I'm working on Mac OS X.

6 Answers 6

8

To turn on colour output from the ls command without having to create an alias to ls or download any additional software, add the following to your ~/.bash_profile:

# Terminal colours
export CLICOLOR=1
export LSCOLORS=ExFxCxDxBxegedabagacad

If you don't like those colours you can use this ls color generator to customize that color list to your liking.

You'll need to do:

source ~/.bash_profile

After making any changes for them to take effect in your existing shell.

1
  • no, it doesn't help at all
    – holms
    Nov 16, 2013 at 3:24
6

The problem is that OS X doesn't have GNU ls; while its ls does support file name coloring, it can only do so by the type of file (file, directory, symlink, device special file, fifo, socket...). Install coreutils from Fink/MacPorts/HomeBrew, then use alias ls='gls --color=auto'.

5
  • Thanks. I've installed coreutils from MacPorts. Should the alias line be in ~/.bashrc? That's where I've put it, but I'm not seeing any difference.
    – AP257
    Mar 22, 2011 at 23:10
  • ~/.bash_aliases may work better. I also found that I needed to run eval $(gdircolors -s) to set it up.
    – geekosaur
    Mar 22, 2011 at 23:12
  • @AP257: On my Mac, I put aliases into the .bash_profile
    – Hai Vu
    Mar 29, 2011 at 2:56
  • @Hai Vu: That only works if you never use subshells.
    – geekosaur
    Mar 29, 2011 at 2:58
  • @geekosaur gdircolors: invalid option -- 's' do you mean --sh ?
    – Anentropic
    Jan 15, 2013 at 12:44
6

BSD ls works a bit differently

alias ls='ls -G'

should work.

And this isn't bash coloring, it is ls doing the colorization. When bash does a file list (try echo * in a directory) there is no way to colorize. Typing ls -G would work in any shell, though a shell (like bash) that has aliases makes it easier.

6

On the Mac, you need to use

export CLICOLOR=1

I put that in the .bash_profile. However, I prefer Rich Homolka's solution to alias ls with -G flag. If you want to customize the colors:

man ls

and search for LSCOLORS

1

Consider installing brew and using the GNU version of ls and other tools.

Install XCode from the AppStore.

Install Homebrew...

/usr/bin/ruby -e "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/master/install)"

Install coreutils...

brew install coreutils

Add to the bottom of your .profile...

export PATH="/usr/local/opt/coreutils/libexec/gnubin:$PATH"
export MANPATH="/usr/local/opt/coreutils/libexec/gnuman:$MANPATH"

I alias ls in my .profile to do colors for everything and a shortcut for full list.

alias ls='ls --color'
alias l='ls -lah'
# -l     use a long listing format
# -a     do not ignore entries starting with .
# -h     with -l and/or -s, print human readable sizes (e.g., 1K 234M 2G)

There are a bunch of LS_COLORS options on github.

-1

bash is only a shell, it does not list files. It executes commands, such as ls.

The --color option (and long options in general) is specific to the version of ls from GNU coreutils, which comes with most Linux distributions. On the other hand, Mac OS X has BSD roots and uses the BSD version of ls which does not support colouring.

1
  • 1
    -1: It does support colouring, just with a different command-line flag: -G.
    – Wuffers
    Mar 29, 2011 at 2:58

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .