ls 命令输出的颜色：它们意味着什么？
ls
![](/data/attachment/album/202308/19/093739lmsxp2psmxxuq9ki.jpg)



> 
> 想知道 ls 命令输出中的颜色是什么？它们从何而来，又该如何设置？本文将为您一一解答。
> 
> 
> 


相信你一定使用过 `ls` 命令来 [列出目录的内容](https://itsfoss.com/list-directory-content/)。在 Ubuntu 和许多其他发行版中，你将看到不同颜色的 `ls` 命令输出。


如果你没有看到过，你可以这样获得如下所示的彩色输出：



```
ls --color=auto

```

但是你有没有想过这些颜色在 `ls` 命令输出中意味着什么？


我将回答本文中的问题。**如果你的终端默认情况下不显示它，我还将展示如何使用 `ls` 命令获取彩色输出**。


### Ubuntu 中 ls 命令中使用的颜色的含义



> 
> ? ls 命令输出的颜色没有固定的标准。不同的终端和发行版使用不同的颜色编码，你也可以根据自己的喜好进行修改。换句话说，不要依赖颜色。
> 
> 
> 


**我在本节中使用默认的 Ubuntu 终端及其颜色配置文件。**


当你 [使用 ls 命令列出文件](https://itsfoss.com/ls-command/) 时，它会使用不同的颜色来指示不同类型的文件。


大多数终端都会以不同的颜色显示可执行文件、链接、常规文件和目录，以便你可以轻松区分它们。


有些终端（例如 Ubuntu 中的默认终端）将其提升到一个新的水平，并为音乐文件、图像和视频添加更多颜色。


为了演示，我列出了来自不同目录的文件，这些文件在 Ubuntu 中用不同的颜色填充了我的终端窗口：


![different colors used by the ls command](/data/attachment/album/202308/19/093825erfuzksrky935ouo.png)


看起来很混乱？ 让我一一为你解密吧！




| 颜色 | 描述 |
| --- | --- |
| **粗体蓝色** | 目录 |
| **无色** | 文件或硬链接 |
| **粗体青色** | 指向文件的符号链接。 |
| **粗体绿色** | 可执行文件（`.sh` 扩展名的脚本） |
| **粗体红色** | 归档文件（主要是 tarball 或 zip 文件） |
| **洋红色** | 表示图像和视频文件 |
| **青色** | 音频文件 |
| **黄色配黑色背景** | 管道文件（称为 FIFO） |
| **粗体红色配黑色背景** | 损坏的符号链接 |
| **无色（白色）配红色背景** | 表示设置用户 ID 文件 |
| **黑色配黄色背景** | 表示设置组 ID 文件 |
| **白色与蓝色背景** | 显示粘滞位目录 |
| **蓝色配绿色背景** | 指向其他可写目录 |
| **黑色配绿色背景** | 当目录同时具有粘滞位和其他可写目录的特征时 |



> 
> ? 再次强调，上述颜色数据基于终端的默认设置，如果更改调色板，将不会得到类似的结果。
> 
> 
> 


但是如果你的终端不显示任何颜色怎么办？ 好吧，这是有原因和解决方案的。


### 如果 ls 命令不显示彩色输出怎么办？


事情是这样的。默认情况下，`ls` 命令不应在输出中显示颜色。如果你使用 `--color=auto` 标志，它将显示颜色。



```
ls --color=auto

```

那么为什么 `ls` 命令在 Ubuntu 和其他一些发行版中默认添加颜色呢？ 这是因为你的发行版为 `ls` 命令设置了别名，以便在执行 `ls` 命令时使用 `--color=auto` 标志：



```
alias

```

![](/data/attachment/album/202308/19/093825krvhxhjaajrbbrbv.png)


因此，如果 `ls` 命令未显示彩色输出，则是因为默认情况下没有设置别名。


现在，每当你使用 `ls` 命令时，你都可以使用 `--color=auto` 标志。


![Use  --color=auto flag with the ls command to get the colored output](/data/attachment/album/202308/19/093825q71qivx3v11zqiig.png)


但这不太方便。相反，你应该创建别名并将其添加到 `.bashrc` 中，以便 `ls` 命令默认显示颜色。


#### 为 ls 创建一个永久别名来显示颜色


要创建永久别名，首先，使用以下命令打开 `.bashrc` 文件：



```
nano ~/.bashrc

```

使用 `Alt + /` [跳到文件末尾](https://linuxhandbook.com/beginning-end-file-nano/) 并 [粘贴如下行到终端](https://itsfoss.com/copy-paste-linux-terminal/):



```
alias ls='ls --color=auto'

```

完成后，[保存更改并退出 nano](https://linuxhandbook.com/nano-save-exit/) 文本编辑器。


要使你刚刚所做的更改生效，请 <ruby> 源引 <rt>  source </rt></ruby> `.bashrc` 文件：



```
source ~/.bashrc

```

就是这样！ 从现在开始，你可以使用彩色输出。


### ls 命令从哪里获取颜色？


现在有趣的部分来了。`ls` 命令的颜色在哪里定义？ 答案是 `LS_COLORS` 环境变量。


是的。这是名为 `LS_COLORS` 的特殊环境变量，它定义 `ls` 命令使用的颜色。


![Value of the LS_COLORS env variable in Ubuntu](/data/attachment/album/202308/19/093825cuylguk78u7zu2jy.png)


这很好，但是谁定义了这个变量呢？ 如果你想做出一些改变怎么办？ 我也来回答一下这些问题。


实际上，你有一个专门的 `dircolors` 命令来为 `ls` 命令设置颜色。


不同的 Shell 有不同的颜色配置文件格式。这就是为什么在使用此命令时应指定 Shell。


![dircolors command output for bash](/data/attachment/album/202308/19/093826hvwxrsoim4gperse.png)


如你所见，它定义了 `LS_COLORS` 环境变量并将其导出，以便该变量可用于子 Shell。


现在，如果你想使用它，你可以将其复制粘贴到你的 `.bashrc` 文件或像这样重定向输出：



```
dircolors -b >> .bashrc

```

并 <ruby> 源引 <rt>  source </rt></ruby> 该文件，以便效果立即可见。你只需要做一次。


#### 理解颜色配置文件


`LS_COLORS` 包含由冒号（`:`）分隔的键值对数据。如果该值有多个部分，则它们之间用分号（`;`）分隔。


键通常是预定义的。值部分代表颜色。


因此，如果显示 `ln=01;36`，则表示对于符号链接，字体为粗体，颜色（`36`）为青色。


`00` 为正常，`01` 为粗体，`4` 为下划线。`31` 代表红色，32 代表绿色等。颜色代码遵循 [ANSI 转义代码](https://en.wikipedia.org/wiki/ANSI_escape_code)。


另一个例子。`or=40;31;01` 表示链接到不存在的文件（键为 `or`），使用黑色背景（颜色代码 `40`）、红色（`31`）和粗体字体（代码 `01`）。


我认为顺序并不重要，因为代码不重叠。`31` 是前景色红色的代码，`41` 是背景红色的颜色。因此，如果使用 `41`，你就知道它用于背景颜色。


### 使用 ls 命令执行更多操作


`ls` 命令可以做更多的事情，为此，我们制作了有关如何使用 `ls` 命令的详细教程：



> 
> **[在 Linux 中使用 ls 命令](https://itsfoss.com/ls-command/)**
> 
> 
> 


Abhishek Prakash 提供资料。


*（题图：MJ/4d0f1f66-259f-4671-a3a8-158f61a38b10）*




---


via: <https://itsfoss.com/ls-color-output/>


作者：[Sagar Sharma](https://itsfoss.com/author/sagar/) 选题：[lkxed](https://github.com/lkxed/) 译者：[geekpi](https://github.com/geekpi) 校对：[wxy](https://github.com/wxy)


本文由 [LCTT](https://github.com/LCTT/TranslateProject) 原创编译，[Linux中国](https://linux.cn/) 荣誉推出


OK
![Warp Terminal](/assets/images/warp-terminal.webp)


![Warp Terminal](/assets/images/warp-terminal.webp)

I'm sure you must have used the ls command to [list the contents of a directory](https://itsfoss.com/list-directory-content/). In Ubuntu and many other distributions, you'll see the ls command output in different colors.

If you don't see it, you can get colored output like this:

`ls --color=auto`


But have you ever wondered what those colors mean in the ls command output?

I'll answer the questions in this article. **I'll also show how to get colored output with ls command** if your terminal does not show it by default.

## Meaning of colors used in the ls command in Ubuntu

**I am using the default Ubuntu terminal and its color profile in this section.**

When you [list files using the ls command](https://itsfoss.com/ls-command/), it will use different colors to indicate different kinds of files.

Most terminals will show the executable files, links, regular files and directories in different colors so that you can easily distinguish between them.

Some terminals, like the default one in Ubuntu, take it to the next level and add more colors for music files, images and videos.

For the demonstrations, I've listed files from different directories which filled my terminal window with different colors in Ubuntu:

![different colors used by the ls command](https://itsfoss.com/content/images/2023/07/meaning-of-different-colors-shown-when-ls-command-is-used-2.png)

Looks confusing? Let me decode each one for you!

Color | Description |
---|---|

Bold Blue |
Directories. |

Uncolored |
File or multi-hard link. |

Bold Cyan |
A symbolic link pointing to a file. |

Bold Green |
An executable file (scripts with having an `.sh` extension). |

Bold Red |
Archive file (mostly a tarball or zip file). |

Magenta |
Indicates images and video files. |

Cyan |
Audio files. |

Yellow with black bg |
A pipe file (known as FIFO). |

Blod red with black bg |
A broken symbolic link. |

Uncolored (white) with red bg |
Indicates set-user-ID file. |

Black with yellow bg |
Indicates set-group-ID file. |

White with blue bg |
Shows a sticky directory. |

Blue with green bg |
Points to Other-writable directory |

Black with green bg |
When a directory has characteristics of both sticky and other-writable directories. |


But what if your terminal doesn't show any colors? Well, there's a reason and solution for that.

## What if the ls command does not show colored output?

Here's the thing. The ls command is not supposed to display colors in output by default. It will show the colors if you use the `--color=auto`

flag.

`ls --color=auto`


Then why does the ls command add colors by default in Ubuntu and some other distributions? That's because your distribution has an alias set for the ls command to use the `--color=auto`

flag when you execute the ls command:

`alias`


![](https://itsfoss.com/content/images/2023/07/reason-behind-ls-uses-colors-by-default-3.png)

So if the ls command is not showing the colorful output, the alias is not set by default.

Now, you may use `--color=auto`

flag whenever you use the ls command

![Use --color=auto flag with the ls command to get the colored output](https://itsfoss.com/content/images/2023/07/Use----color-auto-flag-with-the-ls-command-to-get-the-colored-output.png)

But that's not very convenient. Instead, you should create alias and add it to your bashrc so that ls command displays colors by default.

### Create a permanent alias for ls to display colors

To create a permanent alias, first, open the `.bashrc`

file using the following command:

`nano ~/.bashrc`


[Go to the end of the file](https://linuxhandbook.com/beginning-end-file-nano/) using `Alt + /`

and [paste the following line in the terminal](https://itsfoss.com/copy-paste-linux-terminal/):

`alias ls='ls --color=auto'`


Once done, [save changes and exit from the nano](https://linuxhandbook.com/nano-save-exit/) text editor.

To take effect from the changes you've just made, source the `.bashrc`

file:

`source ~/.bashrc`


That's it! From now on, you can use the colored output.

## Where does the ls command gets the color from?

Now comes the interesting part. Where are the colors for the ls command defined? The answer is LS_COLORS.

Yes. That's the special environment variable called LS_COLORS that defines the colors used by the ls command.

![Value of the LS_COLORS env variable in Ubuntu](https://itsfoss.com/content/images/2023/07/ls_colors-env-variable.png)

That's good but who defines this variable? What if you wanted to make some changes? I'll answer these questions as well.

Actually, you have a dedicated `dircolors`

command to setup color for the ls command.

Different shells have different formats for the color profile. This is why you should indicate the shell when you use this command.

![dircolors command output for bash](https://itsfoss.com/content/images/2023/07/dircolors-command-output.png)

As you can see, it defines the LS_COLORS environment variable and exports it so that the variable is available for the subshell.

Now, if you want to use it, you can either copy-paste it to your bashrc file or redirect the output like this:

`dircolors -b >> .bashrc`


And source the file so that the effects are immediately visible. You have to do it only once.

### Understanding the color profile

The LS_COLORS has data in key-value pair separated by a colon (:). If the value has more than one part, they are separated by a semicolon (;).

The key is usually predefined. The value part represents the colors.

So, if it says `ln=01;36`

, it means for symbolic links, the font is bold and the color (36) is cyan.

0 is for normal, 1 is for bold, 4 is for underlined. 31 is for red, 32 is for green etc. The color codes follow the [ANSI escape code](https://en.wikipedia.org/wiki/ANSI_escape_code).

Another example. `or=40;31;01`

means that link to a non-existent file (key is or) uses black background (color code 40), red color and bold font (code 01).

I think the order doesn't matter because the codes don't overlap. 31 is the code for the foreground red color and 41 is the color for the background red color. So if 41 is used, you know it is for the background color.

## Do more with the ls command

The ls command can do a lot more and for that purpose, we made a detailed tutorial on how to use the ls command:

[Using ls Command in Linuxls is one of the simplest and most frequently used commands is Linux. Learn to use it effectively in this tutorial.](https://itsfoss.com/ls-command/)![](https://itsfoss.com/content/images/2023/07/using-ls-command-in-linux.png)


![](https://itsfoss.com/content/images/2023/07/using-ls-command-in-linux.png)

**This **

**tutorial was requested**by an It's FOSS member in our community forum. If you have suggestions or tutorial requests, please

**use our Community platform**.

*With inputs from Abhishek Prakash.*