Sign up ×
Super User is a question and answer site for computer enthusiasts and power users. It's 100% free, no registration required.

I have a client with a directory tree that has a number of high-level directories that I need to have on my local development machine. I rarely look at them unless something goes awry.

I've marked the "uninteresting" ones with the grey color label and the ones I use daily with a green color label in Finder. That works fine on the rare occasion when I'm using Finder.

Instead, I use, although I am not against other terminal emulators as long as they are fast and robust. When using ls, even with the @ and G flags, I don't have useful information that I can use to de-emphasize the uninteresting directory entries. (If I could pipe ls through a simple awk script to apply coloring, I'd be fine with that.)

I am aware that I can use osascript to get this attribute and then decorate the file output, but that will probably result in the slowest ls implementation since Unicos. I also know I can change the default colors with ls, but that doesn't quite dig to the level that I need.

Is there a simple, fast, tool that already exists that will color ls output based on Finder label colors, and then fall back to $LSCOLORS? Or is that my next github project? :-)


share|improve this question
It's actually quite easy to compile a modified ls as I explain here. It should be possible to replace the color by type with color by attribute. –  Daniel Beck Dec 6 '12 at 6:02

1 Answer 1

up vote 13 down vote accepted

Color information is available from the extended attribute.

$ xattr -p filename
00 00 00 00 00 00 00 00 00 0C 00 00 00 00 00 00
00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00

First row, tenth byte, bits 3-1 (i.e. those with binary values 2, 4 and 8). The example output is for a red file with no other values set.

If the attribute isn't available or the nibble is 0, it's colorless.

Possible values in context-menu order from left to right: 0 (colorless), C (red), E, A, 4, 8, 6, 2 (gray).

Remember to check for possible 1 values for that last bit, i.e. 3 would also be gray + some other attribute.

One possible solution would be the following:

Define a function ls in ~/.bash_profile that does something slightly different if no parameters are given:

function ls {
        if [ "$#" = "0" ] ; then
                find . -maxdepth 1 -exec ~/Library/Scripts/ '{}' \;
                $( which ls ) $@
} could look like the following, calling a Python script and printing the filename depending on its output:



if [ ! -e "$filename" ] ; then

filename="$( basename "$filename" )"

attrs=( $( $( dirname $0 )/ "$filename" | cut -f2 ) )


if [ "$hidden" = "True" ] ; then


case "$color" in
    "none" )
        format="setab 8"
    "red" )
        format="setab 1"
    "orange" )
        format="setab 1"
    "yellow" )
        format="setab 3"
    "green" )
        format="setab 2"
    "blue" )
        format="setab 4"
    "purple" )
        format="setab 5"
    "gray" )
        format="setab 7"

echo "$( tput $format )$filename$( tput sgr0 )"

And, which extracts relevant file attributes, in the same directory:

#!/usr/bin/env python

from sys import argv, exit
from xattr import xattr
from struct import unpack

if len(argv) < 2:
    print('Missing filename argument!')

attrs = xattr(argv[1])

    finder_attrs= attrs[u'']

    flags = unpack(32*'B', finder_attrs)

    hidden = flags[8] & 64 == 64
    color = flags[9] >> 1 & 7
    hidden = False
    color = 0

colornames = { 0: 'none', 1: 'gray', 2 : 'green', 3 : 'purple', 4 : 'blue', 5 : 'yellow', 6 : 'red', 7 : 'orange' }

print 'hidden:\t', hidden
print 'color:\t', colornames[color]

I didn't have a large enough matching color selection here, so red and orange are both printed in red.

enter image description here

enter image description here

I added the hidden attribute here since I am interested in that part of modifying ls output. Just remove the if statement if you don't want it.

share|improve this answer
Thanks, that's a great answer and I think it puts me further on the road to what I need to accomplish. –  Art Taylor Jun 10 '11 at 17:02

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.