介绍
在本实验中,你将学习如何在 Linux 中使用 dircolors 命令来自定义终端中目录和文件列表的颜色方案。dircolors 命令读取一个配置文件,该文件指定了不同文件类型和目录类型的颜色设置,通常位于 ~/.dircolors 或 /etc/DIR_COLORS。你将首先了解 dircolors 命令的用途,然后学习如何自定义目录和文件的颜色,最后管理 dircolors 配置文件。
在本实验中,你将学习如何在 Linux 中使用 dircolors 命令来自定义终端中目录和文件列表的颜色方案。dircolors 命令读取一个配置文件,该文件指定了不同文件类型和目录类型的颜色设置,通常位于 ~/.dircolors 或 /etc/DIR_COLORS。你将首先了解 dircolors 命令的用途,然后学习如何自定义目录和文件的颜色,最后管理 dircolors 配置文件。
在这一步中,你将了解 Linux 中 dircolors 命令的用途。dircolors 命令用于自定义终端中目录和文件列表的颜色方案。
dircolors 命令读取一个配置文件,该文件指定了不同文件类型和目录类型的颜色设置。此配置文件通常位于 ~/.dircolors 或 /etc/DIR_COLORS。
让我们从检查系统当前的颜色设置开始:
dircolors --print-database
示例输出:
## Configuration file for dircolors, a utility to help you set the
## LS_COLORS environment variable used by GNU ls with the --color option.
## Copyright (C) 1996-2022 Free Software Foundation, Inc.
## Copying and distribution of this file, with or without modification,
## are permitted provided the copyright notice and this notice are preserved.
## 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 su
TERM xterm
TERM xterm-color
TERM xterm-debian
TERM rxvt
TERM screen
TERM screen-256color
TERM tmux
TERM tmux-256color
TERM vt100
TERM cons25
TERM fbterm
TERM bay
TERM cygwin
TERM dtterm
TERM dvtm
TERM Eterm
TERM eterm-color
TERM foot
TERM gnome
TERM hurd
TERM jfbterm
TERM kitty
TERM konsole
TERM kterm
TERM lxterminal
TERM st
TERM terminator
TERM tmux-256color
TERM vte
TERM vte-256color
TERM xfce4-terminal
TERM alacritty
TERM alacritty-direct
TERM urxvt
TERM urxvt-256color
TERM screen-256color-bce
## 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
LINK 01;36 ## symbolic link
FIFO 33 ## pipe
SOCK 01;35 ## socket
DOOR 01;35 ## door
BLK 01;33 ## block device driver
CHR 01;33 ## character device driver
ORPHAN 01;05;37;41 ## orphaned symlinks
MISSING 01;05;37;41 ## ... and the files they point to
EXEC 01;32 ## executable file
输出显示了不同文件类型和目录类型的当前颜色设置。你可以通过修改 ~/.dircolors 配置文件来自定义这些设置。
在下一步中,你将学习如何使用 dircolors 命令自定义目录和文件的颜色。
在这一步中,你将学习如何使用 dircolors 命令自定义目录和文件的颜色。
首先,在你的 ~/project 目录中创建一个自定义的 .dircolors 配置文件:
nano ~/.dircolors
将以下内容添加到文件中:
## Custom dircolors configuration
NORMAL 00
FILE 00
DIR 01;32
LINK 01;36
EXEC 01;33
此配置设置了以下颜色方案:
保存并退出文件。
现在,让我们应用新的颜色设置:
eval $(dircolors ~/.dircolors)
你现在应该会在终端中看到目录和文件颜色的变化。
为了使更改永久生效,你可以将 eval $(dircolors ~/.dircolors) 命令添加到你的 ~/.bashrc 或 ~/.zshrc 文件中,具体取决于你使用的 shell。
让我们验证更改:
ls -l ~/project
示例输出:
total 0
drwxr-xr-x 2 labex labex 4096 Apr 18 12:34 [1;32mdirectory[0m
-rw-r--r-- 1 labex labex 0 Apr 18 12:34 [0mfile.txt[0m
lrwxrwxrwx 1 labex labex 5 Apr 18 12:34 [1;36msymlink[0m -> file.txt
-rwxr-xr-x 1 labex labex 0 Apr 18 12:34 [1;33mexecutable[0m
如你所见,目录、符号链接和可执行文件现在以你在 .dircolors 文件中设置的自定义颜色显示。
在这最后一步中,你将学习如何管理 dircolors 配置文件。
dircolors 命令通过读取配置文件来确定目录和文件列表的颜色设置。默认的配置文件位于 /etc/DIR_COLORS,但你也可以使用主目录中的自定义配置文件(~/.dircolors)。
让我们探索管理 dircolors 配置文件的不同方法:
cat /etc/DIR_COLORS
此文件包含系统的默认颜色设置。
nano ~/.dircolors
你可以在主目录中创建一个自定义配置文件(~/.dircolors),并覆盖系统范围的设置。
eval $(dircolors ~/.dircolors)
创建自定义配置文件后,你需要使用 eval 命令应用更改。
echo 'eval $(dircolors ~/.dircolors)' >> ~/.bashrc
为了使自定义配置永久生效,你可以将 eval 命令添加到 shell 的启动文件中(例如 ~/.bashrc 或 ~/.zshrc)。
现在,让我们验证是否正在使用自定义配置:
ls -l ~/project
目录和文件颜色应反映你在 ~/.dircolors 文件中定义的自定义设置。
在本实验中,你将学习 Linux 中 dircolors 命令的用途,该命令用于自定义终端中目录和文件列表的颜色方案。你还将学习如何自定义目录和文件的颜色,以及管理 dircolors 配置文件。实验内容包括理解 dircolors 命令的用途、自定义目录和文件颜色,以及管理 dircolors 配置文件。