Colored LS command in Terminal (configuring)

Discussion in 'macOS' started by ilnyckyj, Jan 6, 2006.

  1. ilnyckyj macrumors member

    May 4, 2005
    Okay, so i wanted my files/folders/filetypes to be displayed in different colors in the OSX terminal as it is done on other *nix systems. So i had a look at this link:

    I got it done, and now i have colors. Only problem is that when something IS colored, it is also put into bold, or increased in size by a slight amount. I know it's not the biggest deal, but i'd like everything to match, but i can't figure out how to edit the colors. I'd also like to add some other filetypes for coloration, but can't figure out where to access the settings. I've even gone through the source for 'ls' and i still can't find it.

    At the bottom of the page i linked to above, someone mentions in a comment that to do this you have to "edit your rc file to export the colors you want as environment variables." I think this refers to an older version of OSX, because i can't find what they're talking about. Anyone know how to do what i'm trying to do?
  2. belvdr macrumors 603

    Aug 15, 2005
    Did you check the new man pages? They usually state this sort of thing.
  3. ilnyckyj thread starter macrumors member

    May 4, 2005
    i did actually, that was one of the things that i tried. The only thing that it mentions is enabling and disabling the colors, not editing them. I'm positive that there is a way to do it, though. It is weird that the man wouldn't have the information.
  4. belvdr macrumors 603

    Aug 15, 2005
    Yeah, I agree. You may have to check the docs that came with the source. Usually README or INSTALL files exist and tell you this.
  5. superbovine macrumors 68030


    Nov 7, 2003
    ummmm.... that is like the hardest way possible to add color to terminal. Anyway, for a better color look in terminal try the built in color function in ls.

    in your .bash_profile add
    alias ls='ls -G'
    here is an example .bash_profile

    cow@supercow cow $cat .bash_profile
    export PATH=/usr/local/mysql/bin:$PATH
    export PATH=~/scripts:$PATH
    export PATH=/usr/local/bin/:$PATH
    alias ls='ls -G'
    PS1="\[\033[0;34m\]\u\[\033[0;30m\]@\[\033[0;31m\]\h \[\033[0;30m\]\W $"
  6. superwoman macrumors regular

    Apr 25, 2005
    I believe you compiled gnu ls. The native Mac OS X ls is BSD ls. They are slightly different.

    In any case, for gnu ls, the color coding is controlled by an environment variable called LS_COLORS. fileutils should also come with a program called dircolors. If yes, do this from a console:

    dircolors >> ~/.bash_profile (or .profile, if you prefer. Man bash to learn the differences between these 2 files.)

    You will see 2 lines appended to you .bash_profile. The "LS_COLORS=" line contains the color codes for the various file types which are separated by colon. Each color code is defined by 2 numbers separated by semi-colon. The first number is the character attributes (e.g. 01 is bold), while the 2nd number is the color (e.g. 31 is red). So "*.tgz=01;31" will list all tgz files as bold red.

    To do what you want, i.e. turn off bold, just make sure that the first number is 00 for each file type you want to change.
  7. semaja2 macrumors 6502a

    Dec 12, 2005

    umm where is this .bash_profile found and is there anything i need to do to make it work
  8. belvdr macrumors 603

    Aug 15, 2005
    It's in your home directory. You can see it via Terminal if you do a:

    ls -la .bash_profile

    If one doesn't exist, you can create it manually with no harm.
  9. Mitthrawnuruodo Moderator emeritus


    Mar 10, 2004
    Bergen, Norway
    TextWrangler has a special option for opening hidden files. Very handy if you want to edit .bash_profile and other "dot-files" without using vi, emacs, pico or whatever text editor you can use in Terminal... :)
  10. ilnyckyj thread starter macrumors member

    May 4, 2005

    this doesn't work though. "-bash: dircolors: command not found" remember, i recompiled the ls command from GNU, but i still have bash from OSX, BSD, which wouldn't have that command.
  11. superbovine macrumors 68030


    Nov 7, 2003
    if you still have the OS X ls command then my post will work.

    you need to edit your .bash_profile, just use ls -G all the time...

    The point is you don't have to recompile anything or download anything color comes with ls out of the box.
  12. ilnyckyj thread starter macrumors member

    May 4, 2005
    Yes, but the problem isn't getting the color to work. I have color working fine, i'd just like to know how to edit the actual colors that are displayed for different file types and folders, and also how to add file types and assign colors to them.
  13. superbovine macrumors 68030


    Nov 7, 2003

    oh my bad... in your .bash_profile as well.

    For more detail info you need to 'man ls' then /CLI your'll find all the info on tweaking the colors.

    export CLICOLOR=1
    # use yellow for directories
    export LSCOLORS=dxfxcxdxbxegedabagacad
  14. superwoman macrumors regular

    Apr 25, 2005
    dircolors is not a bash command, it's another program. Since you don'y have it, then append these 2 lines into your .bash_profile (or .profile) and reopen a new terminal.

    export LS_COLORS
  15. superbovine macrumors 68030


    Nov 7, 2003
    EDIT: THIS ASSUME YOU DIDN'T HAVE THE ORIGINAL 'ls' from OS X oooops (i'll eat crow now....a lot of it)

    ilnyckyj: it think it be easier to get the darwin source code for ls and recompile it back to the way it was a copy ls from a backup. (YUMMM CROW)

    are you sure that is for os x? I believe that syntax comes dircolors from the linux end of things.

    This is from the man for ls file

     LSCOLORS        The value of this variable describes what color to use
                         for which attribute when colors are enabled with
                         CLICOLOR.  This string is a concatenation of pairs of the
                         format fb, where f is the foreground color and b is the
                         background color.
                         The color designators are as follows:
                               a     black
                               b     red
                               c     green
                               d     brown
                               e     blue
                               f     magenta
                               g     cyan
                               h     light grey
                               A     bold black, usually shows up as dark grey
                               B     bold red
                               C     bold green
                               D     bold brown, usually shows up as yellow
                               E     bold blue
                               F     bold magenta
                               G     bold cyan
                               H     bold light grey; looks like bright white
                               x     default foreground or background
                         Note that the above are standard ANSI colors.  The actual
                         display may differ depending on the color capabilities of
                         the terminal in use.
                        The order of the attributes are as follows:
                               1.   directory
                               2.   symbolic link
                               3.   socket
                               4.   pipe
                               5.   executable
                               6.   block special
                               7.   character special
                               8.   executable with setuid bit set
                               9.   executable with setgid bit set
                               10.  directory writable to others, with sticky bit
                               11.  directory writable to others, without sticky
                         The default is "exfxcxdxbxegedabagacad", i.e. blue fore-
                         ground and default background for regular directories,
                         black foreground and red background for setuid executa-
                         bles, etc.

    dircolors on CentOS

    # Configuration file for dircolors, a utility to help you set the
    # LS_COLORS environment variable used by GNU ls with the --color option.
    # The keywords COLOR, OPTIONS, and EIGHTBIT (honored by the
    # slackware version of dircolors) are recognized but ignored.
    # Below, there should be one TERM entry for each termtype that is colorizable
    TERM linux
    TERM linux-c
    TERM mach-color
    TERM console
    TERM con132x25
    TERM con132x30
    TERM con132x43
    TERM con132x60
    TERM con80x25
    TERM con80x28
    TERM con80x30
    TERM con80x43
    TERM con80x50
    TERM con80x60
    TERM dtterm
    TERM xterm
    TERM xterm-color
    TERM xterm-debian
    TERM rxvt
    TERM screen
    TERM screen-w
    TERM vt100
    TERM Eterm
    # Below are the color init strings for the basic file types. A color init
    # string consists of one or more of the following numeric codes:
    # 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
    NORMAL 00       # global default, although everything should be something.
    FILE 00         # normal file
    DIR 01;34       # directory
    LINK 01;36      # symbolic link.  (If you set this to 'target' instead of a
                    # numerical value, the color is as for the file pointed to.)
    FIFO 40;33      # pipe
    SOCK 01;35      # socket
    DOOR 01;35      # door
    BLK 40;33;01    # block device driver
    CHR 40;33;01    # character device driver
    ORPHAN 40;31;01 # symlink to nonexistent file
    # This is for files with execute permission:
    EXEC 01;32
    # List any file extensions like '.gz' or '.tar' that you would like ls
    # to colorize below. Put the extension, a space, and the color init string.
    # (and any comments you want to add after a '#')
    # If you use DOS-style suffixes, you may want to uncomment the following:
    #.cmd 01;32 # executables (bright green)
    #.exe 01;32 01;32
    #.btm 01;32
    #.bat 01;32
    .tar 01;31 # archives or compressed (bright red)
    .tgz 01;31
    .arj 01;31
    .taz 01;31
    .lzh 01;31
    .zip 01;31
    .z   01;31
    .Z   01;31
    .gz  01;31
    .bz2 01;31
    .deb 01;31
    .rpm 01;31
    .jar 01;31
    # image formats
    .jpg 01;35
    .jpeg 01;35
    .gif 01;35
    .bmp 01;35
    .pbm 01;35
    .pgm 01;35
    .ppm 01;35
    .tga 01;35
    .xbm 01;35
    .xpm 01;35
    .tif 01;35
    .tiff 01;35
    .png 01;35
    .mov 01;35
    .mpg 01;35
    .mpeg 01;35
    .avi 01;35
    .fli 01;35
    .gl 01;35
    .dl 01;35
    .xcf 01;35
    .xwd 01;35
    # audio formats
    .ogg 01;35
    .mp3 01;35
    .wav 01;35
  16. ilnyckyj thread starter macrumors member

    May 4, 2005
    okay, so anyone know where i can get the darwin source for ls? because now i have the other, and it would be way easier the other way.
  17. superbovine macrumors 68030


    Nov 7, 2003
  18. superwoman macrumors regular

    Apr 25, 2005
    My bad... I'm using gnu ls compiled from darwinports.

Share This Page