I am trying to get colored listing of directories and files as per their extension after ls command. Recently I switched over to mac leopard from Linux. There is no .bashrc file in my home directory, so I created one with the following contents.This is doing only a bit what I want (only directories as colored). I also want to view files with different extension in different colors. What and where to add? thanx in advance :)
# Define how Bash prompt looks like:
#
# User @ Host - working dir
#export PS1='\u@\h\w$ '
export PS1='\u@\h\w: '
# Cli Colors
export CLICOLOR=1
# use blue for dir’s
export LSCOLORS=exfxcxdxbxegedabagacad
# Alias
#
#
alias ls='ls -al'
The ls that comes with OS X is from BSD, and lacks some of the features of its GNU sibling.
To get the result you want, you need to install coreutils (assuming you have MacPorts installed, then run sudo port install coreutils), which will give you the GNU versions, albeit prefixed by a g (i.e. gls).
Next, run the command gdircolors -p > ~/.dir_colors to generate the file where you will store your colour preferences. It has many common file extensions already present, just add in any missing ones, or change the numbers if you prefer different colours. Or copy an existing one if you already have one from a GNU/Linux installation.
To get your colours to work, first open up .bash_profile and add the lines:
eval `gdircolors ~/.dir_colors`
alias ls="gls --color=auto"
alias ll="gls --color=auto -lh"
alias la="gls --color=auto -a"
Which will overrule the BSD versions, and add colour. Then either run source .bash_profile or open up a new terminal for the changes to take effect. If you get the error bash: dircolors: command not found, then you haven't got gdircolors in your path when it is called, so make sure it appears after the line that says something like
export PATH=/opt/local/bin:/opt/local/sbin:$PATH
eval `gdircolors ~/.dir_colors`
Finally, I've read in places that this might be better placed in your .bashrc file, in which case add the lines to that and add this line at the end of your .bash_profile:
if [ -f ~/.bashrc ]; then . ~/.bashrc; fi
No idea if that's necessary or not, I don't have it and haven't had any problems.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With