lsコマンドの表示色(カラー表示)の設定方法

Last modified: 2021-11-09

lsコマンドでファイルやディレクトリをリスト表示したとき、種類や拡張子によって色が変わるようになっている。

この設定はカスタマイズでき、背景色との関係で見づらい部分を直したり、色の付かない拡張子を色付けできるようにしたりできる。

BusyBoxのlsコマンドでは本記事の方法による表示色のカスタマイズはできない

色を付けるまでの処理の流れ

  1. シェルの初期化スクリプト内で、evalコマンド1dircolorsコマンドを実行する
  2. dircolorsが色の設定を読み込んで、環境変数LS_COLORSをセットするコマンドをシェルに送る2
  3. シェルに変数LS_COLORSが代入される
  4. エイリアス定義などでlsコマンドに--color=auto3オプションを付けるようにすると、環境変数LS_COLORSを参照し、これに従って色を付ける

dircolorsコマンドを使用しなくても、LS_COLORSさえ適切にセットされていれば色設定は行われるが、非常に長くなってしまうため、通常は色設定ファイルを用意してdircolorsコマンドを実行する手順がとられる。

2007年時点では、既定(システム全体)の色設定がファイル/etc/DIR_COLORSに保存されているディストリ(当時のGentoo Linux)もあったが、2021年時点では、基本コマンド集coreutilsのdircolorsコマンドは既定のデータベースファイル(dircolors.hinから生成)を埋め込んでおり、引数なしで実行すると埋め込まれた既定値に基づいてLS_COLORSの値を出力する。

色設定ファイルの作成とカスタマイズ

色設定ファイルの作成

$ dircolors -p > ~/.dircolors

これを行うと、初期設定の色設定データベースファイルをホームディレクトリ内の.dircolorsというファイルに出力する。これを編集することで色設定のカスタマイズができる。

設定ファイルの読み込み

この処理は、シェルの初期化ファイルに記述するとよい。

bash/zsh

bashではディストリによっては自動的に処理される。4

zshの場合、[ホームディレクトリ]/.zshenvに記述するとよい。

[一部]ファイル名:~/.bashrc(bashの場合)または~/.zshenv(zshの場合)
eval $(dircolors -b ~/.dircolors)

fishやCシェル

fishやCシェルではCシェルの文法で変数をセットするために-cオプションを-bの代わりに付ける。

下はfish用の例。$を付けない点に注意。

[一部]ファイル名:${XDG_CONFIG_HOME}/fish/config.fish(XDG_CONFIG_HOMEが定義済みの場合)または~/.config/fish/config.fish(XDG_CONFIG_HOMEが未定義の場合)
eval (dircolors -c ~/.dircolors)

配色・書式の変更

[引用]ファイル名:coreutils-8.32/src/dircolors.hin
# One can use codes for 256 or more colors supported by modern terminals.
# The default color codes use the capabilities of an 8 color terminal
# with some additional attributes as per the following 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

この部分の解説を参考に、;区切りで2桁のコードを指定していく。

コード 効果
00 リセット
01 太字
04 下線
05 点滅(端末によっては効果なし)
07 前景色と背景色を反転
08 前景色を背景色を同じにして隠す
30-37 前景色を黒,赤,緑,黄,青,マゼンタ,シアン,白から選んで指定
38;05;[Num] 前景色を256色パレットのNum番に指定
38;02;[Red];[Green];[Blue] 前景色を24bitカラーで指定(RGB各成分値は0-255)
40-47 背景色を黒,赤,緑,黄,青,マゼンタ,シアン,白から選んで指定
48;05;[Num] 背景色を256色パレットのNum番に指定
48;02;[Red];[Green];[Blue] 背景色を24bitカラーで指定(RGB各成分値は0-255)

このファイルの中に設定例はたくさんあるので、それを参考にしてもOK。

色や属性は重ねて指定でき

DIR 01;04;34;40	# directory

のように、太字・下線・前景色・背景色の同時指定もできる。

ディレクトリは目立つほうがいいと思うので、色か装飾のどちらかでも目立つようにしておくとよいかもしれない。

カスタマイズ例

下は、既定の設定にある程度の修正を加えてみたものを既定のファイルと比較した差分となる。

既定の設定は、lsdircolorsを含むcoreutilsパッケージのバージョンが上がると多少変化するため、このように、修正した部分を残しておくと、新しい設定ファイルをベースに、設定を作り直したりする5場合に役に立つかもしれない。

カスタマイズ例 (2007年7月・差分形式)
--- .dircolors-default
+++ /home/[ユーザ名]/.dircolors
@@ -60,7 +60,7 @@
 # 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
+DIR 01;04;34;40	# directory
 LINK 01;36	# symbolic link.  (If you set this to 'target' instead of a
 		# numerical value, the color is as for the file pointed to.)
 FIFO 40;33	# pipe
@@ -117,8 +117,11 @@
 .cpio 01;31
 .7z  01;31
 .rz  01;31
+.sqfs 01;31
+.sqfs3 01;31
 
 # image formats
+.svg 01;35
 .jpg 01;35
 .jpeg 01;35
 .gif 01;35
@@ -168,6 +171,23 @@
 .log 00;32
 .tex 00;32
 .doc 00;32
+.odt 00;32
+.ods 00;32
+.odp 00;32
+.odb 00;32
+.odg 00;32
+.odf 00;32
+.sgml 00;32
+.dsl 00;32
+.htm 00;32
+.html 00;32
+.xml 00;32
+.xsl 00;32
+.rnc 00;32
+.rng 00;32
+.css 00;32
+.chm 00;32
+.ebuild 00;32
 
 # audio formats
 .aac 00;36
@@ -181,3 +201,16 @@
 .ogg 00;36
 .ra 00;36
 .wav 00;36
+# Configuration Files
+.ini 00;33
+.cfg 00;33
+.conf 00;33
+.reg 00;33
+# *rc 00;33
+# Development Files
+.c 01;33
+.h 01;33
+.cc 01;33
+.cpp 01;33
+.cxx 01;33
+.po 01;33

下は256色パレットの色を用いたカスタマイズ例。

256color-customdircolors

256色パレットを用いたカスタマイズ例 (2008年7月・ファイル全体)
# 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, 1999-2008
# 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.

# You can copy this file to .dir_colors in your $HOME directory to override
# the system defaults.

# Below, there should be one TERM entry for each termtype that is colorizable
TERM Eterm
TERM ansi
TERM color-xterm
TERM con132x25
TERM con132x30
TERM con132x43
TERM con132x60
TERM con80x25
TERM con80x28
TERM con80x30
TERM con80x43
TERM con80x50
TERM con80x60
TERM cons25
TERM console
TERM cygwin
TERM dtterm
TERM eterm-color
TERM gnome
TERM gnome-256color
TERM konsole
TERM kterm
TERM linux
TERM linux-c
TERM mach-color
TERM mlterm
TERM putty
TERM rxvt
TERM rxvt-cygwin
TERM rxvt-cygwin-native
TERM rxvt-unicode
TERM screen
TERM screen-256color
TERM screen-bce
TERM screen-w
TERM screen.linux
TERM vt100
TERM xterm
TERM xterm-16color
TERM xterm-256color
TERM xterm-88color
TERM xterm-color
TERM xterm-debian

# 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;04;38;05;75  # directory
LINK   01;38;05;81  # symbolic link.  (If you set this to 'target' instead of a
                    # numerical value, the color is as for the file pointed to.)
FIFO 38;05;142;48;05;235   # pipe
SOCK 01;38;05;176      # socket
DOOR 01;38;05;176      # door
BLK 38;05;142;48;05;235;01    # block device driver
CHR 38;05;142;48;05;235;01    # character device driver
ORPHAN 01;06;38;05;254;48;05;124  # orphaned syminks
MISSING 01;05;38;05;254;48;05;124 # ... and the files they point to
SETUID 38;05;255;48;05;124	# file that is setuid (u+s)
SETGID 38;05;237;48;05;221	# file that is setgid (g+s)
STICKY_OTHER_WRITABLE 38;05;16;48;05;41 # dir that is sticky and other-writable (+t,o+w)
OTHER_WRITABLE 34;42 # dir that is other-writable (o+w) and not sticky
STICKY 01;38;05;254;48;05;21	# dir with the sticky bit set (+t) and not other-writable

# This is for files with execute permission:
EXEC 01;38;05;47

# List any file extensions like '.gz' or '.tar' that you would like ls
# to colorize below. Put the extension, a space, and the color init string.
# (and any comments you want to add after a '#')

# If you use DOS-style suffixes, you may want to uncomment the following:
#.cmd 01;32 # executables (bright green)
#.exe 01;32
#.com 01;32
#.btm 01;32
#.bat 01;32
# Or if you want to colorize scripts even if they do not have the
# executable bit actually set.
#.sh  01;32
#.csh 01;32

# archives or compressed (bright red)
.tar 01;38;05;168
.tgz 01;38;05;168
.svgz 01;38;05;168
.arj 01;38;05;168
.taz 01;38;05;168
.lzh 01;38;05;168
.lzma 01;38;05;168
.zip 01;38;05;168
.z   01;38;05;168
.Z   01;38;05;168
.dz  01;38;05;168
.gz  01;38;05;168
.bz2 01;38;05;168
.bz  01;38;05;168
.tbz2 01;38;05;168
.tz  01;38;05;168
.deb 01;38;05;168
.rpm 01;38;05;168
.jar 01;38;05;168
.rar 01;38;05;168
.ace 01;38;05;168
.zoo 01;38;05;168
.cpio 01;38;05;168
.7z  01;38;05;168
.rz  01;38;05;168

.sqfs 01;38;05;168
.sqfs3 01;38;05;168

# image formats
.jpg 01;38;05;170
.jpeg 01;38;05;170
.gif 01;38;05;170
.bmp 01;38;05;170
.pbm 01;38;05;170
.pgm 01;38;05;170
.ppm 01;38;05;170
.tga 01;38;05;170
.xbm 01;38;05;170
.xpm 01;38;05;170
.tif 01;38;05;170
.tiff 01;38;05;170
.png 01;38;05;170
.svg 01;38;05;170
.mng 01;38;05;170
.pcx 01;38;05;170
.mov 01;38;05;170
.mpg 01;38;05;170
.mpeg 01;38;05;170
.m2v 01;38;05;170
.mkv 01;38;05;170
.ogm 01;38;05;170
.mp4 01;38;05;170
.m4v 01;38;05;170
.mp4v 01;38;05;170
.vob 01;38;05;170
.qt  01;38;05;170
.nuv 01;38;05;170
.wmv 01;38;05;170
.asf 01;38;05;170
.rm  01;38;05;170
.rmvb 01;38;05;170
.flc 01;38;05;170
.avi 01;38;05;170
.fli 01;38;05;170
.gl 01;38;05;170
.dl 01;38;05;170
.xcf 01;38;05;170
.xwd 01;38;05;170
.yuv 01;38;05;170

# document files
.pdf 00;38;05;76
.ps 00;38;05;76
.txt 00;38;05;76
.patch 00;38;05;76
.diff 00;38;05;76
.log 00;38;05;76
.tex 00;38;05;76
.doc 00;38;05;76

.chm 00;38;05;76
.css 00;38;05;76
.dsl 00;38;05;76
.ebuild 00;38;05;76
.htm 00;38;05;76
.html 00;38;05;76
.odb 00;38;05;76
.odf 00;38;05;76
.odg 00;38;05;76
.odp 00;38;05;76
.ods 00;38;05;76
.odt 00;38;05;76
.rnc 00;38;05;76
.rng 00;38;05;76
.sgml 00;38;05;76
.xml 00;38;05;76
.xsl 00;38;05;76

# audio formats
.aac 00;38;05;44
.au 00;38;05;44
.flac 00;38;05;44
.gsf 00;38;05;44
.h2song 00;38;05;44
.mid 00;38;05;44
.midi 00;38;05;44
.mka 00;38;05;44
.mp3 00;38;05;44
.mpc 00;38;05;44
.ogg 00;38;05;44
.ra 00;38;05;44
.rg 00;38;05;44
.tta 00;38;05;44
.wav 00;38;05;44

# configuration Files
.cfg 00;38;05;227
.conf 00;38;05;227
.ini 00;38;05;227
.reg 00;38;05;227

# development Files
.c 01;38;05;209
.cc 01;38;05;209
.cpp 01;38;05;209
.cxx 01;38;05;209
.h 01;38;05;209
.po 01;38;05;209

.gmo 01;38;05;216
.ko 01;38;05;216
.mo 01;38;05;216
.o 01;38;05;216

# base64
.b64 00;38;05;79

  1. 引数をシェルのコマンドとして実行するコマンド ↩︎

  2. 既定ではbashなどのBシェル系のシェルが解釈するexportコマンドを出力し、tcshなどのCシェル系のシェルまたはfishでは-cオプションを付け、setenvコマンドを送る必要がある ↩︎

  3. --color--color=alwaysの強制指定だと、ファイルにリダイレクトした場合などにも色を変えるためのエスケープシーケンスが含まれてしまうのでauto指定が最適 ↩︎

  4. Debian/Ubuntuのbashでは、初めてbashを使用したときに自動生成されるbash設定ファイルが用いられていると[ホームディレクトリ]/.dircolorsが自動的に読み込まれる ↩︎

  5. このファイルを作り直す必要性は基本的にはないため、あまりそういったことをする人はいない気もするが… ↩︎