设定ls指令的显示颜色
时间:2007-01-26 来源:hmilycbz
dircolors [OPTION]... [FILE]
指令說明
這是指令相當特別,她主要功能在於輸出一段關於 ls 指令顯示顏色的變數設定值。目前支援 bash 與 tcsh,在 linux 環境中,相當於支援 sh, bash, csh, tcsh
選項說明
-b, --sh, --bourne-shell :列出 bash (sh) 的 LS_COLORS 變數設定
-c, --csh, --c-shell :列出 tcsh (csh) 的 setenv LS_COLORS 環境變數設定
-p, --print-database :列出設定檔案的內容。
--help :顯示程式用法資訊
--version :顯示程式本身的版本資訊
RedHat 7.3 中,系統有 bash 的預設環境,檔案是 /etc/bashrc ,其中有這麼一段描述
if [ "x$SHLVL" != "x1" ]; then # We're not a login shell
for i in /etc/profile.d/*.sh; do
if [ -r "$i" ]; then
. $i
fi
done
fi
是這段讓你在 login 後每開啟新的 shell 時都去讀取與執行 /etc/profile.d 目錄下的 *.sh。在當中,有一個相關於 dircolors 的 shell script,名稱是 colorls.sh。她的內容如下
# cat -n colorls.sh
1 # color-ls initialization
2 COLORS=/etc/DIR_COLORS
3 eval `dircolors --sh /etc/DIR_COLORS`
4 [ -f "$HOME/.dircolors" ] && eval `dircolors --sh $HOME/.dircolors` && COLORS=$HOME/.dircolors
5 [ -f "$HOME/.dir_colors" ] && eval `dircolors --sh $HOME/.dir_colors` && COLORS=$HOME/.dir_colors
6
7 if echo $SHELL |grep bash 2>&1 >/dev/null; then # aliases are bash only
8 if ! egrep -qi "^COLOR.*none" $COLORS &>/dev/null; then
9 alias ll='ls -l --color=tty'
10 alias l.='ls -d .[a-zA-Z]* --color=tty'
11 alias ls='ls --color=tty'
12 else
13 alias ll='ls -l'
14 alias l.='ls -d .[a-zA-Z]*'
15 fi
16 fi
17
#
第二行,設定變數 COLORS,給於初始值 /etc/DIR_COLORS。
第三行,以 shell 的內建指令 eval 執行 dircolors 的輸出。到此初步完成系統的預設值。
第四、五行,檢測使用者的 home 目錄下是否有自己的 dircolors 參數設定檔 ".dircolors" 或
".dir_colors",如果有,再次使用 eval 執行 dircolors 的設定。到此完成使用者自己的 dircolors 設定。
第七到十六行,設定 ls 指令的別名(aliases)。
以上即是 bash 運作預設的 dircolors 流程。認知,理解這個流程,有助於掌握設定。接下來讓我們來看設定 dircolors 顏色顯示的資料檔,樣本是安裝時檢附的原始設定檔案 /etc/DIR_COLORS。
[root@www etc]# cat -n DIR_COLORS
1 # Configuration file for the color ls utility
2 # This file goes in the /etc directory, and must be world readable.
3 # You can copy this file to .dir_colors in your $HOME directory to override
4 # the system defaults.
5
6 # COLOR needs one of these arguments: 'tty' colorizes output to ttys, but not
7 # pipes. 'all' adds color characters to all output. 'none' shuts colorization
8 # off.
9 COLOR tty
10
11 # Extra command line options for ls go here.
12 # Basically these ones are:
13 # -F = show '/' for dirs, '*' for executables, etc.
14 # -T 0 = don't trust tab spacing when formatting ls output.
15 OPTIONS -F -T 0
16
17 # Below, there should be one TERM entry for each termtype that is colorizable
18 TERM linux
19 TERM console
20 TERM con132x25
21 TERM con132x30
22 TERM con132x43
23 TERM con132x60
24 TERM con80x25
25 TERM con80x28
26 TERM con80x30
27 TERM con80x43
28 TERM con80x50
29 TERM con80x60
30 TERM cons25
31 TERM xterm
32 TERM rxvt
33 TERM xterm-color
34 TERM color-xterm
35 TERM vt100
36 TERM dtterm
37 TERM color_xterm
38
39 # EIGHTBIT, followed by '1' for on, '0' for off. (8-bit output)
40 EIGHTBIT 1
41
42 # Below are the color init strings for the basic file types. A color init
43 # string consists of one or more of the following numeric codes:
44 # Attribute codes:
45 # 00=none 01=bold 04=underscore 05=blink 07=reverse 08=concealed
46 # Text color codes:
47 # 30=black 31=red 32=green 33=yellow 34=blue 35=magenta 36=cyan 37=white
48 # Background color codes:
49 # 40=black 41=red 42=green 43=yellow 44=blue 45=magenta 46=cyan 47=white
50 NORMAL 00 # global default, although everything should be something.
51 FILE 00 # normal file
52 DIR 01;34 # directory
53 LINK 01;36 # symbolic link
54 FIFO 40;33 # pipe
55 SOCK 01;35 # socket
56 BLK 40;33;01 # block device driver
57 CHR 40;33;01 # character device driver
58 ORPHAN 01;05;37;41 # orphaned syminks
59 MISSING 01;05;37;41 # ... and the files they point to
60
61 # This is for files with execute permission:
62 EXEC 01;32
63
64 # List any file extensions like '.gz' or '.tar' that you would like ls
65 # to colorize below. Put the extension, a space, and the color init string.
66 # (and any comments you want to add after a '#')
67 .cmd 01;32 # executables (bright green)
68 .exe 01;32
69 .com 01;32
70 .btm 01;32
71 .bat 01;32
72 .sh 01;32
73 .csh 01;32
74 .tar 01;31 # archives or compressed (bright red)
75 .tgz 01;31
76 .arj 01;31
77 .taz 01;31
78 .lzh 01;31
79 .zip 01;31
80 .z 01;31
81 .Z 01;31
82 .gz 01;31
83 .bz2 01;31
84 .bz 01;31
85 .tz 01;31
86 .rpm 01;31
87 .cpio 01;31
88 .jpg 01;35 # image formats
89 .gif 01;35
90 .bmp 01;35
91 .xbm 01;35
92 .xpm 01;35
93 .png 01;35
94 .tif 01;35
[root@www etc]#
以上這個檔案格式說明,清楚地紀錄在 On-line manual dir_colors (5) 當中,想詳細參閱的人可使用 man 指令查閱
# man 5 dir_colors
顏色與號碼的對應,源自於 ISO 6429 裡的定義。參考的資料入下
0 to restore default color
1 for brighter colors
4 for underlined text
5 for flashing text
30 for black foreground
31 for red foreground
32 for green foreground
33 for yellow (or brown) foreground
34 for blue foreground
35 for purple foreground
36 for cyan foreground
37 for white (or gray) foreground
40 for black background
41 for red background
42 for green background
43 for yellow (or brown) background
44 for blue background
45 for purple background
46 for cyan background
47 for white (or gray) background
事實上,你會發現原檔案的 44 ~ 49 行歸類的比較清楚,以下,我直接用他們來加上中文。
44 # Attribute codes: # 顯示屬性碼
45 # 00=預設值 01=粗體 04=底線 05=閃爍 07=反轉 08=隱藏
46 # Text color codes: # 文字顏色碼
47 # 30=黑 31=紅 32=綠 33=黃 34=籃 35=紅紫 36=墨綠 37=白
48 # Background color codes: # 文字底色碼
49 # 40=黑 41=紅 42=綠 43=黃 44=籃 45=紅紫 46=墨綠 47=白
這是顏色定義的部份。
50 ~ 59 行,是相關於何種檔案對應何種顏色的描述。66 ~ 94 行,是關於文件副檔名分類的顏色對應。這兩者的設定格式相同
檔案型態簡碼 色碼:色碼:色碼
所有詳細的檔案型態簡碼可在 dir_colors 中查得,不再贅述。色碼雖有三種型態但沒有重複,所以使用上最多三個,可以不分次序,但必須以";"加以隔開。
想建立自己的顏色設定,最好還是將 /etc/DIR_COLORS 拷貝一份到自己的 home 目錄,檔名命名為
".dircolors"或".dir_colors",然後使用 vi 修改自己的檔案即可。原範例檔案(/etc/DIR_COLORS )是最佳樣本。有興趣、有需要時,不訪試試自己調調看。當然,別忘了,有"拷貝",就有"保佑"。