참으로 깔끔하게 잘 정리해놨네. Redhat 계열인 Fedora 나 CentOS 같은 경우는 /etc/DIR_COLORS 스크립트가 있지만, ubuntu 는 그런게 없네. 조금 특이한 방법이기는 하나, 아래와 같이 해서 잘 사용할 수 있다.

Lightbulb Howto: Add custom color to directory listings.

You ever fire up an xterm and perform an "ls" command? Sure you have! If so, did you ever wish that:
  • certain files would "stand out" with a custom color, and not the defaults?
  • you can add other filename extensions to the database with their own custom color as well?
Well, here's how...

NOTE: All commands to be entered in a terminal shell or changes made to a file are hilighted in red. You only need to cut/paste those items which are hilighted, the surrounding text is left for illustration purposes.

1. Edit the '.bashrc' file. You need to make a few small changes to the existing bash script.
  1. (optional) Backup the file. Copy your existing '.bashrc' file in case you wish to restore it at a latter time. For example,
    Code:
    skoal@morpheus:///tmp $ cd && cp .bashrc .bashrc~
    skoal@morpheus://~ $ ls .bashrc*
    .bashrc  .bashrc~
  2. Modify the file. Using your favorite text editor, open the file '~/.bashrc' and make the following changes hilighted in red (which should appear somewhere near the top of that file - line 17 if no prior alterations were made). You will basically be modifying one line and adding two more above it.
    Code:
    skoal@morpheus:///tmp $ gedit ~/.bashrc
    and make these changes in the file,
    Code:
    # enable color support of ls and also add handy aliases
    if [ "$TERM" != "dumb" ]; then
        [ -e "$HOME/.dircolors" ] && DIR_COLORS="$HOME/.dircolors"
        [ -e "$DIR_COLORS" ] || DIR_COLORS=""
        eval "`dircolors -b $DIR_COLORS`"
        alias ls='ls --color=auto'
        #alias dir='ls --color=auto --format=vertical'
        #alias vdir='ls --color=auto --format=long'
    fi
  3. (alternative to step 1.ii) Patch the file. Using the attched 'bashrc.patch' file, patch the current '~/.bashrc' file instead of cutting/pasting with an editor.
    Code:
    skoal@morpheus:///tmp $ bunzip2 bashrc.patch.bz2
    skoal@morpheus:///tmp $ cp bashrc.patch ~
    skoal@morpheus:///tmp $ cd && patch -p0 < bashrc.patch
    patching file .bashrc
    Hunk #1 succeeded at 19 (offset 5 lines).
2. Create the '.dircolors' file. The '.dircolors' file is created by using the 'dircolors' program. It will generate a default color scheme to be used with the "LS_COLORS" environment variable, which gives the colored output while using "ls".
Code:
skoal@morpheus:///tmp $ cd && dircolors -p > .dircolors
3. Edit the '.dircolors' file. Here is where you will be modifying/creating new color schemes to reflect your own unique style.

NOTE: Lines 34-41 within this file give you the color codes for three specific keys: attribute, text, and background. You can choose to use all three keys, or simply pick and choose only those keys you wish to apply. Look at some of the pre-defined ones you may already recognize as a good example to follow.
  1. Modify existing color schemes. For example, I can't stand big bold blue directory listings, so I change the bold attribute from "01" to "00" (none) but keep the blue color. It will lighten things up a bit.
    Code:
    FILE 00         # normal file
    DIR 00;34       # directory
    LINK 01;36      # symbolic link.
    or, change the "01" to an "07", making your debian packages really stand out!
    Code:
    .gz  01;31
    .bz2 01;31
    .deb 07;31
    .rpm 01;31
    .jar 01;31
    which will "reverse" the red (31) foreground color - basically making it black text inside a red box! Whoa! Ugly but effective, no?

  2. Create new color schemes. I like to program, yet for some reason, there are no default colors for well known language extensions. I like my c/c++ source to show up as green and my header files as yellow. Here's how I added these extensions to the bottom of that file:
    Code:
    # audio formats
    .ogg 01;35
    .mp3 01;35
    .wav 01;35
    
    # programming languages
    .c 00;32
    .cc 00;32
    .cpp 00;32
    .h 00;33
4. Source the '.bashrc' file. In order for your changes to take effect within the current shell, you need to source the '.bashrc' file.
Code:
skoal@morpheus:///tmp $ cd && . .bashrc
** END of Howto **

and, just to see your handy work, look at these two environmental variables:
Code:
skoal@morpheus://~ $ set | grep 'DIR_COLORS\|LS_COLORS'
DIR_COLORS=/home/skoal/.dircolors
LS_COLORS='no=00:fi=00:di=00;34:ln=01;36:pi=40;33:so=01;35:do=01;35:bd=40;33;01:cd=40;33;01:or=40;31;01:ex=01;32:*.tar=01;31:*.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:*.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:*.ogg=01;35:*.mp3=01;35:*.wav=01;35:*.c=00;32:*.cc=00;32:*.cpp=00;32:*.h=00;33:'
NOTE: These color changes will be preserved upon each reboot or login. If you wish to revert to the original color scheme, just delete the file '.dircolors' in your $HOME directory (or, alternatively, restore your backup file made in step 1.i).
Code:
skoal@morpheus:///tmp $ rm -f ~/.dircolors
NOTE: I've only tested this with a plain 'ole vanilla stock 'xterm'. I do not use gnome-terminal, kterm(?), or any other variants but it should work equally well with them. I do not use any file managers of any sort, so without some MIME associated icon staring me in the face, these colors really help. Using 'dircolors' is old as dirt, and I'm surprised no one else hadn't mentioned this yet. Am I the only one who thinks CLI+color > [insert favorite File Manager here]? Surely, I'm not alone. Or am I...

\\//_

Last edited by skoal; June 13th, 2005 at 09:28 PM..
반응형
블로그 이미지

Good Joon

IT Professionalist Since 1999

,