Submit Hint Search The Forums LinksStatsPollsHeadlinesRSS
14,000 hints and counting!

10.3: Colored output from ls (revisited) UNIX
Under older versions of OS X you had to download a custom version of ls in order to get colored file listings (see this older hint). This custom version also used a helper program called dircolors which created color preferences from a .dircolorsfile.

Under Panther, this can all be done now with the built-in version of ls, and without the dircolors program. Instead of the --color flag, the built-in ls uses the -G flag or the CLICOLOR environment variable. The color preferences can be set using the LSCOLORS environment variable. For full information, browse the man page with man ls. Note the man page says that "d" is brown when used in LSCOLORS, but in fact it is yellow. Yellow is nice to use for directories when you have a black terminal background.

I use tcsh, and I have the following in my ~/.cshrc file:

setenv CLICOLOR 1
# use yellow for directories
setenv LSCOLORS dxfxcxdxbxegedabagacad

The syntax for bash would be slightly different, to go into a .bashrc file:

export CLICOLOR=1
# use yellow for directories
export LSCOLORS=dxfxcxdxbxegedabagacad

There might be more official locations to put custom shell settings (something in the Library directory?) but I'm from the Unix world so I still use .cshrc. Anyway, once you use colored ls, you will never go back!

[robg adds: I decided to run this one, even though it's also discussed in a recent hint, as that hint may have been too tcsh-specific (and too complex!) for 10.3.]

  • Currently 3.00 / 5
  You rated: 1 / 5 (10 votes cast)

10.3: Colored output from ls (revisited) | 21 comments | Create New Account
Click here to return to the '10.3: Colored output from ls (revisited)' hint
The following comments are owned by whoever posted them. This site is not responsible for what they say.
10.3: Colored output from 'ls' (revisited)
Authored by: sharumpe on Oct 31, '03 10:53:50AM

Something that's nice about this is that it colors directories that are world-writable differently. I appreciate the visual cue -- makes it easy to spot (and therefore correct) that kind of thing.

Mr. Sharumpe

[ Reply to This | # ]
And bash...
Authored by: zedwards on Oct 31, '03 11:02:47AM

I am hoping that this will be addressed in future hints, but do we put aliases in the .bashrc? or .aliases? I am confused now, after using bash in linux, then tcsh in osx then the "change" in 10.2.

[ Reply to This | # ]
10.3: Colored output from 'ls' (revisited)
Authored by: ChiefTypist on Oct 31, '03 11:15:38AM

In order for this feature to work, you must uncheck the "Disable ANSI color" in the Color preferences of the Terminal inspector.

[ Reply to This | # ]
10.3: Colored output from 'ls' (revisited)
Authored by: cubixe on Oct 31, '03 01:19:22PM
I prefer the "old" way of getting color in the terminal. The gnu ls that the fileutils package from fink installs has, IMHO, a better way of specifying colors. The gnu version lets you set colors for most (if not all) of the things panther's ls does, but with gnu ls you can also set different colors for files ending in different extensions. For example,
setenv LS_COLORS "ex=35:di=36:ln=31:*.c=32:*.cpp=32:*.cc=32:*.cxx=32:*.h=33:*.hh=33:*.m=32"
This is great for easily distinguishing source files, header files, etc. To answer a post above regarding .alias and .cshrc and .bashrc files... its really a personal preference thing. I have all my aliases in a .alias file. If you do that, you must include a line in the .cshrc file such as:
source $HOME/.alias
The .cshrc (or .bashrc for bash) file is what is sourced when the shell is executed, so to get the effects of the .alias file, you must explicitly source it. You can certainly put your aliases in .cshrc (or .bashrc), but this just keeps things modular and makes it easier to work with.

[ Reply to This | # ]
10.3: Colored output from 'ls' (revisited)
Authored by: flunkedflank on Oct 31, '03 01:27:37PM

By the way, as the submitter of this hint, I'll throw in one more bonus, not necessarily for the faint-of-heart. I have a colored prompt as well, enabled as such in .cshrc:

set prompt = "%{\033[30;43m%}%~ %%%{\033[0m%} "

I find colored prompts invaluable, as they make it easy to visually distinguish between the output of multiple commands. (Got hooked on it from xwsh on IRIX that used a yellow prompt by default.)

Dig through "man tcsh" and find the definition of the prompt variable. You'll find:

%{string%} - Includes string as a literal escape sequence.

The color settings are determined by the numbers between "\033[" (ESC-[) and "m". They are defined as such:

# Attribute codes:
# 00=none 01=bold 04=underscore 05=blink 07=reverse 08=concealed
# Text color codes:
# 30=black 31=red 32=green 33=yellow 34=blue 35=magenta 36=cyan 37=white
# Background color codes:
# 40=black 41=red 42=green 43=yellow 44=blue 45=magenta 46=cyan 47=white

Any combination of numbers can be used, separated by a semicolon. So in my case I have "30;43", which means black text on a yellow background. Then I reset it to normal with the "0" value at the end.

Mmm ... gotta love terminal escape sequence crap.

[ Reply to This | # ]
10.3: Colored output from 'ls' (revisited)
Authored by: ajr on Oct 31, '03 04:14:44PM

This works in, but does not seem to work in X11.

Any ideas?

[ Reply to This | # ]
10.3: Colored output from 'ls' (revisited)
Authored by: fds on Oct 31, '03 08:48:02PM

You just have to set the TERM environment variable from the default "xterm" to "xterm-color" which is what the Apple uses.

So for example in bash:
export TERM="xterm-color"

In iTerm you can set the TERM value in the configuration dialog too.

[ Reply to This | # ]
Shell Environment in Mac OS X
Authored by: thinkyhead on Nov 01, '03 02:25:29AM

The proper place to put your customized tcsh settings in Mac OS X is in the folder ~/Library/init/tcsh in files called:

  • aliases.mine
  • completions.mine
  • environment.mine
  • login.mine
  • logout.mine
  • path
  • rc.mine

The scripts in /usr/share/tcsh/examples set up your base environment, then you create the above files to add your own overrides and customizations. Type cat /usr/share/tcsh/examples/README for the details.

| slur was here

[ Reply to This | # ]

Shell Environment in Mac OS X
Authored by: customjake on Nov 05, '03 12:13:40AM
While these may be 'correct' places to place these commands, i prefer the .profile. The .profile file is more compatible with other shells. I came to this conclusion after consulting several *nix support sites.

There's my 2 cents

[ Reply to This | # ]
10.3: Colored output from 'ls' (revisited)
Authored by: beelers on Nov 01, '03 01:18:03PM
Easiest answer for bash users, which is the default for clean Panther installs... In, create (or add to) .bashrc in home directory add the line
alias "ls"="ls -G"
Save the file, then type source .bashrc at the prompt. Now, typing ls will show colors.

no sig is here

[ Reply to This | # ]

10.3: Colored output from 'ls' (revisited)
Authored by: osxfan57 on Jun 15, '04 12:59:39AM

I tried this - just installed Panther clean 2 weeks ago and have made no changes - just added a few apps. This doesn't work. The output it still black on light yellow background - don't have disable ANSI output checked either. Hmmm.

iMac 17, OS 10.3

[ Reply to This | # ]
same here
Authored by: alanb1979 on Jun 15, '04 06:08:53PM

Same deal here (dual 800 G4, 10.3.whateveristhelatest).

Checking the man page for ls, mine says that the -G is to "inhibit display of group information". However, it does mention that the --color option is available ... and that works correctly for me.

Any suggestions for a nice color scheme with a white background?

hiedy hoo

[ Reply to This | # ]
10.3: Colored output from 'ls' (revisited)
Authored by: habesct on Nov 01, '03 02:59:04PM

My aliase to 'ls -alG | more' does not work. I can get colors in my tcsh shell (and bash) if I type 'ls -alG', but I would really like the contents to pipe through more. If I try to pipe it, I don't get colors anymore... almost as if it just sees 'ls -la | more'

Any thoughts?


[ Reply to This | # ]
10.3: Colored output from 'ls' (revisited)
Authored by: flunkedflank on Nov 02, '03 03:40:41AM

Yes! Check the man page. There is an environment variable you must set, I think it's CLICOLOR_FORCE. Note that this might cause ls to *break* in certain situations. I noticed it broke a gnu configure script I was running at one point. Workaround so that it doesn't break other things: modify your alias to set the variable, run the command, then unset the variable. (yes, a bit cumbersome.)

[ Reply to This | # ]
10.3: Colored output from 'ls' (revisited)
Authored by: tfoss on Nov 04, '03 05:52:43PM

You also need to tell more to translate the escape sequences into colors. The -r option of more (or less) should do that, so just a simple 'alias more more -r" should work.


[ Reply to This | # ]
10.3: Colored output from 'ls' (revisited)
Authored by: derekhed on Jun 15, '04 08:10:48PM

Awesome hint. Thanks all!

[ Reply to This | # ]
10.3: Colored output from ls (revisited)
Authored by: lovethedj on Nov 05, '03 02:19:44AM

ok, I now have colored ls, but how do I now adjust the colors???

the LSCOLORS variable lets you associate file and directory types with colors, but I would like to know how to specify the "ANSI" colors using RGB values. The default colors are just bad!!!


[ Reply to This | # ]
10.3: Colored output from ls (revisited)
Authored by: lovethedj on Feb 04, '04 05:23:36PM

Ok, I figured out how to assign different colors to types in terminal - simply open the color picker (cmd+shift+c), pick a color, drag and drop the color from the color swatch on top of the text you want to change. Repeat for each type ... cool - but there are problems:

1) how do you modify the background color?
2) These changes are not saved as the default colors - how do say make these my default colors?


[ Reply to This | # ]
10.3: Non-bold text in
Authored by: osteslag on Mar 29, '04 09:32:54AM

Does anybody know how to make not showing its text in bold per default?

I bought a beautiful monospaced font (yes, they exist!) to use in the Terminal, but it's displaying a boldish variant of it. The same happens if I select the built-in Courier and Monaco.

If I uppercase the color codes for LSCOLORS (which normally means bold), the file and directory names are actually shown in normal text when running ls. But everything else is still bold. I'm running 10.3.3.

[ Reply to This | # ]
10.3: Non-bold text in
Authored by: blakers on Jun 15, '04 05:59:35PM


just curious ... what monospaced font did you find that you're happy with?

[ Reply to This | # ]
10.3: Colored output from ls (revisited)
Authored by: DMCrimson on May 10, '05 09:01:11AM

the above works well in 10.4, but I've my own gripes...
on my old environmernt.mine I had the following:

(*** \ means the line continues. !!)
Is there any way to convert it to the new ls? or should I go on and compile fileutils, installing just ls...

[ Reply to This | # ]