80

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?

1

9 Answers 9

85

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.

2
  • 8
    Ditto. I've alias ls='ls -G' set in my .bashrc on Snow Leopard.
    – ayaz
    Sep 2, 2010 at 15:40
  • For some reason, CLICOLOR=Y stopped working on my Mac. alias ls='ls -G' would force ls to colorize. I define this for interactive terminals only. Oct 15, 2018 at 22:49
38

Open the terminal window and type:

alias ls='ls -G'

Then hit Enter and done!

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.

2
  • 1
    The output says: If you really need to use these commands with their normal names, you can... Why does it emphasise really? What are the downsides?
    – z0r
    Nov 8, 2017 at 22:11
  • And you never archived your page on archive.org and now your link in your last paragraph is gone
    – barlop
    Apr 4, 2019 at 11:32
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
  • 6
    Can you explain this code?
    – bwDraco
    Feb 17, 2015 at 17:29
  • 1
    setting CLICOLOR environment variable for *BSD and Darwin systems - if it's set ls and possibly other utilities would work colored, but GNU ls (for Linux) ignores it. If "ls --color=auto" will not fail (exit status =0) - we have GNU version of ls and making alias to draw color codes in interactive mode, if it fails - then we don't need alias because of CLICOLOR variable. "&> /dev/null" just don't show stderr and stdout if something fail or if it's ok. Works for my linux and osx. (p.s. bash on osx and freebsd doesn't read .bashrc, so put it in .profile. already fixed it). Feb 23, 2015 at 17:33
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.

4
  • 4
    That's overkill, in my opinion. You don't need to install a separate version of ls when the same feature is supported slightly differently on the existing version of ls.
    – ayaz
    Sep 2, 2010 at 15:41
  • Good point. Then again, colored ls has never been my taste. alias ls='ls -F' Sep 2, 2010 at 17:37
  • alias ls='ls -FG' -- it's the best of both worlds!
    – mipadi
    Sep 2, 2010 at 17:42
  • 1
    I do this using homebrew. It's not overkill if you use both Mac and *nix computers and want your terminals to look the same -- this allows you to use the same config files across all computers. It's installed as gls and doesn't replace the original so there's really no downside.
    – senderle
    Mar 31, 2014 at 13:14
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.

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
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

1
  • I added that to my $PATH recently and I can't get the GNU ls to show color without explicitly doing ls --color=auto.. eval $(dircolors) set the LS_COLORS environment variable as expected but still no color without --color. ideas?
    – adfaklsdjf
    Feb 8, 2022 at 17:49
0

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.

You must log in to answer this question.

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