[linux_shell]linux下設置ls命令顯示文件、文件夾顏色


轉自:http://ruiming.i.sohu.com/blog/view/238600241.htm

linux下設置ls命令顯示文件、文件夾顏色:

1. LS_COLORS

用 dircolors -p  可以 看到缺省的顏色設置,包括各種顏色和“粗體”,下划線,閃爍等定義。

discolors -p
  1 $ dircolors -p
  2 # Configuration file for dircolors, a utility to help you set the
  3 # LS_COLORS environment variable used by GNU ls with the --color option.
  4 # The keywords COLOR, OPTIONS, and EIGHTBIT (honored by the
  5 # slackware version of dircolors) are recognized but ignored.
  6 # Below, there should be one TERM entry for each termtype that is colorizable
  7 TERM linux
  8 TERM linux-c
  9 TERM mach-color
 10 TERM console
 11 TERM con132x25
 12 TERM con132x30
 13 TERM con132x43
 14 TERM con132x60
 15 TERM con80x25
 16 TERM con80x28
 17 TERM con80x30
 18 TERM con80x43
 19 TERM con80x50
 20 TERM con80x60
 21 TERM cygwin
 22 TERM dtterm
 23 TERM putty
 24 TERM xterm
 25 TERM xterm-color
 26 TERM xterm-debian
 27 TERM rxvt
 28 TERM screen
 29 TERM screen-bce
 30 TERM screen-w
 31 TERM vt100
 32 TERM Eterm
 33 # Below are the color init strings for the basic file types. A color init
 34 # string consists of one or more of the following numeric codes:
 35 # Attribute codes:
 36 # 00=none 01=bold 04=underscore 05=blink 07=reverse 08=concealed
 37 # Text color codes:
 38 # 30=black 31=red 32=green 33=yellow 34=blue 35=magenta 36=cyan 37=white
 39 # Background color codes:
 40 # 40=black 41=red 42=green 43=yellow 44=blue 45=magenta 46=cyan 47=white
 41 NORMAL 00 # global default, although everything should be something.
 42 FILE 00 # normal file
 43 DIR 01;34 # directory
 44 LINK 01;36 # symbolic link. (If you set this to 'target' instead of a
 45  # numerical value, the color is as for the file pointed to.)
 46 FIFO 40;33 # pipe
 47 SOCK 01;35 # socket
 48 DOOR 01;35 # door
 49 BLK 40;33;01 # block device driver
 50 CHR 40;33;01 # character device driver
 51 ORPHAN 40;31;01 # symlink to nonexistent file
 52 SETUID 37;41 # file that is setuid (u+s)
 53 SETGID 30;43 # file that is setgid (g+s)
 54 STICKY_OTHER_WRITABLE 30;42 # dir that is sticky and other-writable (+t,o+w)
 55 OTHER_WRITABLE 34;42 # dir that is other-writable (o+w) and not sticky
 56 STICKY 37;44 # dir with the sticky bit set (+t) and not other-writable
 57 # This is for files with execute permission:
 58 EXEC 01;32
 59 # List any file extensions like '.gz' or '.tar' that you would like ls
 60 # to colorize below. Put the extension, a space, and the color init string.
 61 # (and any comments you want to add after a '#')
 62 # If you use DOS-style suffixes, you may want to uncomment the following:
 63 #.cmd 01;32 # executables (bright green)
 64 #.exe 01;32
 65 #.com 01;32
 66 #.btm 01;32
 67 #.bat 01;32
 68 .tar 01;31 # archives or compressed (bright red)
 69 .tgz 01;31
 70 .arj 01;31
 71 .taz 01;31
 72 .lzh 01;31
 73 .zip 01;31
 74 .z 01;31
 75 .Z 01;31
 76 .gz 01;31
 77 .bz2 01;31
 78 .deb 01;31
 79 .rpm 01;31
 80 .jar 01;31
 81 # image formats
 82 .jpg 01;35
 83 .jpeg 01;35
 84 .gif 01;35
 85 .bmp 01;35
 86 .pbm 01;35
 87 .pgm 01;35
 88 .ppm 01;35
 89 .tga 01;35
 90 .xbm 01;35
 91 .xpm 01;35
 92 .tif 01;35
 93 .tiff 01;35
 94 .png 01;35
 95 .mov 01;35
 96 .mpg 01;35
 97 .mpeg 01;35
 98 .avi 01;35
 99 .fli 01;35
100 .gl 01;35
101 .dl 01;35
102 .xcf 01;35
103 .xwd 01;35
104 # audio formats
105 .flac 01;35
106 .mp3 01;35
107 .mpc 01;35
108 .ogg 01;35
109 .wav 01;35

環境變量LS_COLORS時用來設置GNU ls的--color選項中的顏色的。

# 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

故,要設置ls的顏色,則在SHELL的配置文件(以下是tcsh的配置文件:.tcshrc,對於bash略有不同)中添加下面一行:

setenv LS_COLORS 'no=00:fi=00:di=01;33:ln=01;36:pi=40;33:so=01;35:bd=40;33;01:cd=40;33;01:or=01;05;37;41:mi=01;05;37;41:ex=01;35:*.cmd=01;35:*.exe=01;35:*.com=01;35:*.btm=01;35:*.bat=01;35:*.sh=01;35:*.csh=01;35:*.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:*.bz=01;31:*.tz=01;31:*.rpm=01;31:*.cpio=01;31:*.jpg=01;35:*.gif=01;35:*.bmp=01;35:*.xbm=01;35:*.xpm=01;35:*.png=01;35:*.tif=01;35:'

即可,可以參照上面的英文自行配置。

另:/etc/DIR_COLORS 文件為系統默認顏色參數配置文件
如果想修改定義自己的顏色參數配置文件,請執行如下操作:
1) cp /etc/DIR_COLORS $HOME/.dir_colors
2) 修改中的顏色定義 $HOME/.dir_colors

2. CLICOLOR

 

# CLICOLOR是用來設置是否進行顏色的顯示。CLI是Command Line Interface的縮寫。
setenv CLICOLOR 1

# LSCOLORS是用來設置當CLICOLOR被啟用后,各種文件類型的顏色
setenv LSCOLORS gxfxaxdxcxegedabagacad

 

LSCOLORS的值中每兩個字母為一組,分別設置某個文件類型的文字顏色和背景顏色。LSCOLORS中一共11組顏色設置,按照先后順序,分別對以下的文件類型進行設置:

  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 bit

LSCOLORS中,字母代表的顏色如下:

  • a 黑色
  • b 紅色
  • c 綠色
  • d 棕色
  • e 藍色
  • f 洋紅色
  • g 青色
  • h 淺灰色
  • A 黑色粗體
  • B 紅色粗體
  • C 綠色粗體
  • D 棕色粗體
  • E 藍色粗體
  • F 洋紅色粗體
  • G 青色粗體
  • H 淺灰色粗體
  • x 系統默認顏色

所以,如果我們想把目錄顯示成紅色,就可以把LSCOLORS設置為bxfxaxdxcxegedabagacad就可以了

////////////////////////////////////////////

LSCOLORS 的含義:

    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
    bit

    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.

 

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM