Shell – Set ls color listings based on regex instead of globbing

colorslsshell

Is it possible to set regex patterns for color matching in the LS_COLORS variable? So instead of just

*.jpg=38;5;220

Can I do

\.(jpg|gif)=38;5;220

That's just an example, I'd like to get more complicated than that. Am I asking too much from this? Is there another way to do terminal color schemes that I can get fancier?

I'm using zsh btw, so if I can do it there but not bash, that's fine.

Best Answer

That's a feature of the ls utility in GNU coreutils. It doesn't depend on the shell. The syntax is not documented (you're supposed to use dircolors and figure it from there), but a quick look at the source shows that the only way to match files by name is to use * followed by a string which must be a suffix for a file name to match. The string is interpreted literally, except that \ and ^ introduce escape sequences (e.g. \n = ^J = newline).

So you can't match different extensions with the same pattern. But you can use shell programming constructs to build LS_COLORS:

for ext in jpg gif; do LS_COLORS="$LS_COLORS:*.$ext=38;5;220"; done