Blog

WSL File and Directory Colors

If you've been following along with my other console guides on WSL, Cmder, and Zsh, you might've noticed that the result of ls looks frankly... disgusting:

Some of this is unavoidable: Windows has files that Linux isn't able to access, and ls will always complain. However, the colors are something we can address with a little legwork on out part.

What's the Problem?

There's two contributing factors here: one having to do with ls itself, and the other having to do with funky WSL internals.

The first is easy: ls has no idea we've set up colors in Cmder. Our Zsh setup is actually aliasing ls to ls --color=tty, which is why we are seeing colors at all, but even Zsh isn't aware of what we've done with Cmder.

The second factor is trickier. To make Linux able to understand the Windows filesystem, there's a variety of workarounds in place.

  • The most obvious and prevalent one is WSL seeing files and directories as being executable. This is partially addressed by the wsl.conf file I suggested adding in my guide to installing WSL, since that addition of metadata fixes some permissions. It doesn't quite fix all of them, though.
  • WSL also can't properly understand Windows symbolic links, so it sees them as being broken or orphaned.

ls is, of course, optimized for normal Linux computers, where executability or broken links would be the most important trait of an entry. Here, though, so many things are executable and so many links are broken that it doesn't have any meaning.

Customizing ls Colors

Starting from a file

If you're using solarized dark like I am, I'll go through instructions here for starting from a file that will fix a lot of things already. If you are using a different color scheme, you can certainly follow along or skip to the next section.

Sebastian Tramp has made a great starting point for solarized dark (or light) directory colors. I prefer his dircolors.ansi-dark file, but these instructions are the same for the other dircolors. files he has in the repository.

  1. Download the file of your choice to our Linux home folder so it doesn't bother us: curl https://raw.githubusercontent.com/seebi/dircolors-solarized/master/dircolors.ansi-dark --output ~/.dircolors
  2. Load in the file in upon startup by editing ~/.zshrc with nano or vim and adding the following lines:
# Set up dircolors
eval `dircolors ~/.dircolors`
  1. Reload the changes with source ~/.zshrc

Now to see the changes:

Well, still ugly, but at least now the ugliness is readable.

Tweaking colors

If you skipped the above part, I recommend looking up instructions on changing directory colors since you won't have a place already set up for you to change colors.

The process for editing colors is fairly simple: edit the file that the colors are being sourced from and then re-source .zshrc. However, I suggest you use vim: it not might be your text editor of choice, but it has automatic syntax highlighting for this so you can see colors as you type them.

Another tip is that the file from the above section has comments in it on what different colors are and where they're useful. If you skipped that section, the article I linked has similar info.

That all being said, my customization steps were as follows:

  1. Run vim ~/.dircolors (different if you skipped the file section)
  2. Set ORPHAN and MISSING to be 01;32
  3. Set EXEC to be 00

That results in the following output from ls:

Much better!

GuidesJack WarrenConsole