当前位置: 首页>>技术教程>>正文


如何在控制台中使用ls更改目录的颜色?

, , ,

问题描述

在我的控制台上,目录的颜色很蓝,在黑暗的背景下很难阅读。

如何更改ls的颜色定义?

最佳解决方案

要更改目录颜色,请使用编辑器打开~/.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

所有可能的颜色:

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 – Configuring LS_COLORS提供更完整的列表。

尤其是,您可能需要更改ow(OTHER_WRITABLE)的背景颜色,该文件的默认值为绿色背景上的non-readable non-bold蓝色文本,以便可供所有用户使用的文件。

您可以在.bashrc文件末尾使用例如LS_COLORS="$LS_COLORS:ow=1;33",以在黑色背景上获得可读的粗体橙色文本。

在更改.bashrc文件后,要使更改生效,必须重新启动shell或运行source ~/.bashrc

资源:

次佳解决方案

很简单。将这三行添加到〜/.bashrc

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

如果您想在正在运行的bash会话中应用更改,请运行:

source ~/.bashrc

第三种解决方案

LS_COLORS

这是一组扩展名:颜色映射,适合用作LS COLORS环境变量。

第四种方案

除Hegazi的回答之外,您可以使用dircolors命令实际控制目录颜色和许多其他颜色。您可以创建一个有据可查的配置文件。

您可以在主目录中创建.dircolor文件,如下所示:

dircolors -p > ~/.dircolors

然后在〜/.bashrc文件中添加行

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

这将为bash创建一个$ LS_COLORS变量。如果用于csh,-c标志将被设置。它还标记ls命令以彩色显示。

在〜/.dircolor文件颜色中编辑上述DIR属性的值,以更改目录(或任何其他颜色的其他包含文件类型)的颜色。您还可以更改特定文件的颜色,或定义您自己的文件。

参考资料

本文由Ubuntu问答整理, 博文地址: https://ubuntuqa.com/article/495.html,未经允许,请勿转载。