Tuesday, July 31, 2012

colored ls

Colored ls


옛날 내 Opera Blog에서 가져온 것입니다. 중복된 내용이나, 한 Blog에 필요한 정보를 수집하기 위함임을 밝힙니다.




말이 필요 없다. 내가 지금 사용 중인 환경을 한 번 보자.


보기 괜찮은가? 이것에 한 번 도전해 보자! 

어렵다면 어렵지만 사실 크게 어렵지도 않다. 주로 참고한 웹페이지는 아래와 같다.

http://hocuspokus.net/2008/01/a-better-ls-for-mac-os-x/



천천히 따라해 보자.

준비물 :

  1. macport
  2. your favorite editor


# macport 에 관해서는 지난 blog를 참고하자.

[MacPorts 소개 및 간단한 사용법] 지난 Post 바로가기


첫째, macport로 [coreutils] package를 설치하자.


$sudo port install coreutils +with_default_names

<--- 여기서 “+with_default_names”는 왜 붙여주는지 정확히는 모르겠으나 그냥 하자. 원문을 읽어보면 원래 깔려있는 commands와 새로 깔리는 commands 사이에 중복을 피하기 위함인 듯이 보인다. 시간이 걸린다. 기다리자.


둘째, 몇몇 파일을 손으로 직접 수정해야 하는데 우선 중요한 몇 가지를 확인하자.

Home Directory(~) 안에 있어야 하는 파일들 :

      1. .bashrc
      2. .dir_colors

$ls -la로 확인해 보고 없으면 만들자. 예를 들어 .bashrc가 없으면

$touch ~/.bashrc <--- ~ 는 기본적으로 Home Directory를 의미하는데 만일 모든 유저에게 다 적용하려면 Home Directory(~) 말고, Root(/)에 하자. 즉,

$touch /.bashrc


# 주의 사항 :

---------------------------------------------------------------------------------

보통 ~ 안에 .bash로 시작하는 몇 개의 파일이 있는데, 이를 조금 조정해 주어야한다. 여러 방법이 있지만 내가 한 방법을 보면 다른 응용도 가능할 것으로 믿는다. 

꼭 확인해 봐야 할 것들 : 

“.bash_profile”는 있으면 지우진 말고, editor를 열어 아래와 같이 적어준다.

    if [ -f ~/.bashrc ]; then

       source ~/.bashrc

    fi

아래 그림을 참고하자. 이는 Aquamacs로 본 .bash_profile이다.


추측하건대, .bash로 시작하는 파일들을 부르는 순서가 있는 듯 하다. 위 script는 .bashrc가 있으면 .bash_profile은 무시하고 .bashrc를 source로 쓰라는 말로 보인다.


“.bash_aliases”가 있을 것이다. (물론 없을 수도 있다) 여기에는 보통 자주 쓰는 commands를 간단히 재 지정하여 bash shell에서 쓰고 싶을 때, 보통 여기에 적어 놓는데 그냥 지워 버리고, 이미 사용자가 지정한 aliases가 있다면 .bashrc에 옮겨버리자! 

---------------------------------------------------------------------------------





셋째, 사용자의 favorite editor로 .bashrc를 열어 아래와 비슷(?)하게 적어주자.


# MacPorts

export PATH=/opt/local/libexec/gnubin:/opt/local/bin:/opt/local/sbin:$PATH

# export PATH=/opt/local/bin:/opt/local/sbin:$PATH

export MANPATH=/opt/local/share/man:$MANPATH


# Terminal colours (after installing GNU coreutils)

NM="\[\033[0;38m\]" #means no background and white lines

HI="\[\033[0;37m\]" #change this for letter colors

HII="\[\033[0;31m\]" #change this for letter colors

SI="\[\033[0;36m\]" #this is for the current directory

IN="\[\033[0m\]"


export PS1="$NM[ user_ID $HII@ $SI\w$NM ] $IN"


if [ "$TERM" != "dumb" ]; then

    export LS_OPTIONS='--color=auto'

    eval `dircolors ~/.dir_colors`

fi


# Useful aliases

alias ls='ls $LS_OPTIONS -hF'

# alias ll='ls $LS_OPTIONS -lhF'

# alias l='ls $LS_OPTIONS -lAhF'


제 경우, Aquamacs로 보면 아래와 같다.



여기서 가장 중요한 부분은

alias ls='ls $LS_OPTIONS -hF'

이 곳인데, 다른 필요한 aliases는 아래에 이어서 적어주자. 



넷째, 사용자의 favorite editor로 .dir_colors를 열어 아래와 비슷(?)하게 적어주자.

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

TERM xterm-color

TERM xterm-debian

TERM rxvt

TERM screen

TERM screen-w

TERM vt100


# 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 00;36   # directory

LINK 01;37  # 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;35


# 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

#.com 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

.dmg 01;31


# image formats

.jpg 01;35

.png 01;35

.gif 01;35

.bmp 01;35

.ppm 01;35

.tga 01;35

.xbm 01;35

.xpm 01;35

.tif 01;35

.mpg 01;35

.avi 01;35

.fli 01;35

.gl 01;35

.dl 01;35


# source code files

# .pl 00;33

# .PL 00;33

# .pm 00;33

# .tt 00;33

# .yml 00;33

# .sql 00;33

# .html 00;33

# .css 00;33

# .js 00;33

.m 00;33

.tex 00;33

.bib 00;33

.f 00;33

.c 00;33


일부분을 보면 아래와 같다.


이는 잘 알려진 확장자를 가진 파일들의 색깔을 넣어주는 것인데, 어떻게 수정하냐 하면, 그림 계열들에 지금 pdf가 빠져 있는데 이를 넣고 싶으면

.pdf 01;35

를 첨가해 주면 된다. 여기서 01;35가 색깔을 지칭하게 되는데 위에 jpg등을 이미 01;35로 지정했으므로 pdf도 이 색깔로 맞춰주었다. 마찬가지로 자주 쓰는 확장자를 한 계열을 정해 지정해 놓으면 그 색깔로 ls 실행 시 보여 지게 된다. 

# 주의사항 :

---------------------------------------------------------------------------------

이때 색깔은 bash shell(terminal)의 바탕색과 겹치어 보여지게 됨을 주의 하자. 맨 위의 첨부한 bash shell에서 실제로 보이는 색을 보면 황토색이 있는데, 이는 사실 노란색으로 지정한 것인데 바탕이 검은색이라 그리 보이는 것이다. 

뭔 숫자가 무슨 색인지는 .dir_colors안에 쓰여 있으니, 몇 가지를 시도해 보면 확인 가능하다. 이때는 bash shell을 완전히 종료 후, 재 시작해야 적용된다.


---------------------------------------------------------------------------------






여기까지 하면, 거의(?) 잘 적용되어야 한다. 이제 bash shell(terminal)을 완전 종료하고 재 시작해 보자. 그리고 ls를 쳐 보면 색깔이 잘 지정되어 나옴을 알 수 있다.


# 마지막 주의사항 :

---------------------------------------------------------------------------------


1) 위에 .bashrc에 써야 하는 사항 중에 아래와 같은 부분이 있다.

# MacPorts

export PATH=/opt/local/libexec/gnubin:/opt/local/bin:/opt/local/sbin:$PATH

# export PATH=/opt/local/bin:/opt/local/sbin:$PATH

export MANPATH=/opt/local/share/man:$MANPATH

가끔 위의 방법으로 했는데 안되는 경우가 있는데, 그럼 위의 첫째 줄과 둘째 줄을 바꿔주자. 

# MacPorts

# export PATH=/opt/local/libexec/gnubin:/opt/local/bin:/opt/local/sbin:$PATH

export PATH=/opt/local/bin:/opt/local/sbin:$PATH

export MANPATH=/opt/local/share/man:$MANPATH

경우에 따라 macport로 깔리는 coreutils package의 경로가 다른 경우가 있는데 이를 변경하는 것이다.


2) 위에 .bashrc에 써야 하는 사항 중에 아래와 같은 부분이 있다.

export PS1="$NM[ user_ID $HII@ $SI\w$NM ] $IN"

위와 그대로 했다면, bash shell에서 맨 상단에 첨부한 그림에서 회색칠로 안 보이게 한 부분에 “user_ID” 가 찍히고 보이게 된다. 즉 프롬프트에서

user_ID @ path of the current directory $

으로 보이게 되는데 본인이 보이게 하고 싶은 글귀를 user_ID에 그냥 적어 넣거나 아래 웹페이지를 참고하자.


https://wiki.archlinux.org/index.php/Color_Bash_Prompt




한글로 뭔가 정보를 주고 싶었는데 이게 내가 하고도 좀 복잡해 보이긴 한다. 충분한 설명이 못 될까 죄송스럽다.

기본 원리를 다시 간단히 정리하면, macport로 coreutils를 깔고 몇 가지 규칙을 조금(?!) 이해한 후 적용하는 것이 사실 다이다. 

No comments:

Post a Comment