如何在控制台中使用 ls 變更目錄的顏色?

如何在控制台中使用 ls 變更目錄的顏色?

在我的控制台上,目錄的顏色是藍色的,很難在深色背景上閱讀。

如何更改 的顏色定義ls

答案1

若要變更目錄顏色,~/.bashrc請使用編輯器開啟文件

nano ~/.bashrc

並在文件末尾添加以下條目:

LS_COLORS=$LS_COLORS:'di=0;35:' ; export LS_COLORS

一些不錯的顏色選擇(在本例中0;35為紫色)是:

Blue = 34
Green = 32
Light Green = 1;32
Cyan = 36
Red = 31
Purple = 35
Brown = 33
Yellow = 1;33
Bold White = 1;37
Light Grey = 0;37
Black = 30
Dark Grey= 1;30

第一個數字是樣式(1=粗體),後面是分號,然後是顏色的實際編號,可能的樣式(效果)是:

0   = default colour
1   = bold
4   = underlined
5   = flashing text (disabled on some terminals)
7   = reverse field (exchange foreground and background color)
8   = concealed (invisible)

可能的背景:

40  = black background
41  = red background
42  = green background
43  = orange background
44  = blue background
45  = purple background
46  = cyan background
47  = grey background
100 = dark grey background
101 = light red background
102 = light green background
103 = yellow background
104 = light blue background
105 = light purple background
106 = turquoise background
107 = white background

所有可能的顏色:

30  = black
31  = red
32  = green
33  = orange
34  = blue
35  = purple
36  = cyan
37  = grey
90  = dark grey
91  = light red
92  = light green
93  = yellow
94  = light blue
95  = light purple
96  = turquoise
97  = white

這些甚至可以組合起來,這樣參數如下:

di=1;4;31;42

在您的 LS_COLORS 變數中將使目錄以粗體下劃線紅色文字顯示,綠色背景!

要在終端機中測試所有這些顏色和样式,您可以使用以下方法之一:

for i in 00{2..8} {0{3,4,9},10}{0..7}
do echo -e "$i \e[0;${i}mSubdermatoglyphic text\e[00m  \e[1;${i}mSubdermatoglyphic text\e[00m"
done

for i in 00{2..8} {0{3,4,9},10}{0..7}
do for j in 0 1
   do echo -e "$j;$i \e[$j;${i}mSubdermatoglyphic text\e[00m"
   done
done

使用 ls 命令時,您也可以透過定義每個檔案來變更其他類型的檔案:

bd = (BLOCK, BLK)   Block device (buffered) special file
cd = (CHAR, CHR)    Character device (unbuffered) special file
di = (DIR)  Directory
do = (DOOR) [Door][1]
ex = (EXEC) Executable file (ie. has 'x' set in permissions)
fi = (FILE) Normal file
ln = (SYMLINK, LINK, LNK)   Symbolic link. If you set this to ‘target’ instead of a numerical value, the color is as for the file pointed to.
mi = (MISSING)  Non-existent file pointed to by a symbolic link (visible when you type ls -l)
no = (NORMAL, NORM) Normal (non-filename) text. Global default, although everything should be something
or = (ORPHAN)   Symbolic link pointing to an orphaned non-existent file
ow = (OTHER_WRITABLE)   Directory that is other-writable (o+w) and not sticky
pi = (FIFO, PIPE)   Named pipe (fifo file)
sg = (SETGID)   File that is setgid (g+s)
so = (SOCK) Socket file
st = (STICKY)   Directory with the sticky bit set (+t) and not other-writable
su = (SETUID)   File that is setuid (u+s)
tw = (STICKY_OTHER_WRITABLE)    Directory that is sticky and other-writable (+t,o+w)
*.extension =   Every file using this extension e.g. *.rpm = files with the ending .rpm

更完整的清單可在Bigsoft - 設定 LS_COLORS

在某些發行版上,您可能還想更改ow“( OTHER_WRITABLE) 的背景顏色,其預設值是不可讀的”,例如,更改為綠色背景上的非粗體藍色文字。

例如,您可以LS_COLORS="$LS_COLORS:di=1;33"在文件末尾使用.bashrc,在黑色背景上獲得可讀的粗體橙色文字。

更改 .bashrc 檔案後,要使變更生效,您必須重新啟動 shell 或執行source ~/.bashrc.

注意:您可以將更多命令與冒號, 例如

LS_COLORS=$LS_COLORS:'di=1;33:ln=36' ; export LS_COLORS; ls

來源:

答案2

很簡單。將這三行加入 ~/.bashrc

$ vi ~/.bashrc
export LS_OPTIONS='--color=auto'
eval "$(dircolors -b)"
alias ls='ls $LS_OPTIONS'

如果您想在正在運行的 bash 會話中套用更改,請執行:

source ~/.bashrc

答案3

除了 Hegazi 的回答之外,您實際上可以使用 dircolors 命令控制目錄顏色以及許多其他顏色。您可以建立一個有詳細記錄的設定檔。

您可以在主目錄中建立一個 .dircolor 文件,如下所示:

dircolors -p > ~/.dircolors

然後在 ~/.bashrc 檔案中加入以下行

eval "`dircolors -b ~/.dircolors`"
alias ls='ls --color=auto'

這將為 bash 建立一個 $LS_COLORS 變數。 -c 標誌將為 csh 設定。它還標記 ls 命令以顏色顯示。

編輯 ~/.dircolor 檔案顏色中 DIR 屬性的值,以變更目錄的顏色(或任何其他包含的檔案類型的其他顏色)。您也可以變更特定文件的顏色,或定義您自己的顏色。

答案4

LS_COLORS

這是擴展的集合:顏色映射,適合用作 LS COLORS 環境變數。

相關內容