81

I'm trying to move my bash configuration from Ubuntu to Mac OS X and it looks like ls is slightly different. For instance, it won't accept the --color option.

How do I get this to work?

Giacomo1968
  • 58,727
cfischer
  • 9,163

9 Answers9

87

ls is actually separate from Bash. Mac OS X has a BSD version of ls, which requires -G on the command line, or CLICOLOR (and perhaps LSCOLORS) in the environment.

See man ls for more info.

Giacomo1968
  • 58,727
39

Open the terminal window and type:

alias ls='ls -G'

Then hit Enter and done!

Indrek
  • 24,874
13

Use Homebrew.

brew install coreutils

Note that this will throw a prefix of g in front of all the commands (e.g., gls for ls). It gives an option to source a file that will alias these for you automatically.

I wasn't sure if there was an option to install them directly without having to do the whole alias thing, so instead in installed MacPorts and did this.

Glorfindel
  • 4,158
10

compatibility for GNU and *BSD/darwin ls

~/.profile

#for *BSD/darwin
export CLICOLOR=1

ls --color=auto &> /dev/null && alias ls='ls --color=auto' ||

~/.bashrc (I don't remember if bash on Linux always reads ~/.profile, but not my zsh on ARCH)

[[ -f $HOME/.profile ]] && source $HOME/.profile
2

You'll need to install an alternate version of ls. The one usually used in linux is from the GNU coreutils project.

You could build and install or install from macports, fink or homebrew.

Doug Harris
  • 28,397
1
  1. On Linux:
alias ls='ls --color'
  1. On MacOS:
alias ls='ls -G'

Combine 2 into 1, you can input this code into .bashrc (on both Linux & MacOS).

myos="$(uname)"
case $myos in
Linux) alias ls='ls --color-auto';;
Darwin) alias ls=ls -G';;
*);;
esac
Glorfindel
  • 4,158
1

macOS now uses zsh for the default terminal. To use ls with color output edit or create the .zshrc file in your home folder and add this:

export CLICOLOR=1

The other requirement is that ls requires a color terminal declaration, such as xterm-16color or xterm-256color in your terminal application, or it won't bother trying to do color output.

CyberSkull
  • 1,505
1

I use this Perl script I wrote on AIX. It’s useful if you’re on a system that doesn’t support --color, and also where you don’t have sudo to install packages.

Should work on Macintosh too.

Giacomo1968
  • 58,727
0

Use homebrew install coreutils:

brew install coreutils

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

useful link:https://github.com/sorin-ionescu/prezto/issues/966