Skip to content

[Linux] Use “ls” command to display different colors and orders according to different file extensions

For a long time, I have felt that the sorting and colors of the files displayed in the terminal are not really beautiful. I am always wondering if there is a good method can color the files in the terminal and sort them according to the file extensions.

After all, sometimes it feels really messy!

After a little study, I record my configuration here.


edit .dircolors file

I have written an article ([Linux] How to Change File Color in Terminal) about changing the color of folders in Linux terminal before. Today I directly use the file color adjusted by others.

wget https://raw.github.com/trapd00r/LS_COLORS/master/LS_COLORS -O ~/.dircolors


And insert the following command in ~/.bashrc file and use source command to start using this configuration.

eval $(dircolors ~/.dircolors)
source ~/.bashrc


You can see richer colors using the ls command.

looks like this


If you are still not satisfied with these colors, you can also edit the ~/.dircolors file.


For example, I adjusted the folder color back to the original default color.


Save the configuration and use source command again.

source ~/.bashrc

In this way, I will see that the folder color has changed. By analogy, you can change any file extensions color you want.


Change the file sorting displayed by ls

Even if I change a nice color, my terminal display is still a bit messy. (MY BAD)

看起來仍舊很亂

At this time, we can use ls --sort=extension to display by file extension.

ls --sort=extension

Output:

how is it? Is it better?

If we want the same sorting in the future, we can go to ~/.bashrc and modify our ls command.

We will find this line of command (IF NOT FOUND, YOU NEED TO ADD IT). At the same time, we change this line of command:

to

alias ls='ls --color=auto --sort=extension'



Save and exit. We still have to use the following command as usual:

source ~/.bashrc

After that, our “ls” command will be sorted by file extensions.


References

Tags:

Leave a Reply