1

I'm modifying my .bashrc: I want to make it so that .zip,.tar and .tar.gz files (and maybe more later down the road) are shown with a red color. Thing is, I want to match for all those files once. So on the .bashrc file I have:

LS_COLORS='*.hs=35:*.py=93:*.[zip|tar.*]=31'
export LS_COLORS

If possible, I would like to avoid

LS_COLORS='*.hs=35:*.py=93:*.zip=31:*.tar=31:*.tar.gz=31'
export LS_COLORS

However, my method is not working. What should I modify?

7
  • 1
    I don't think you can do that with LSCOLORS, that's its own syntax. We could help you generate the full string, I guess, but it's probably easier, faster and simpler to just write it out.
    – terdon
    Commented Aug 12, 2022 at 14:43
  • Really? How strange, I would've guessed that had to be feature.That's a bit shame. Thanks for the reply! Btw, does *.tar.* work? (as in everything before .tar and everything after). Because it is not working for me Commented Aug 12, 2022 at 14:46
  • I don't know how ls handles these internally, but *.tar.* might work since it is a standard, portable globbing pattern. [zip|tar.*] is not. Also note that these are not regular expressions, they are glob patterns. *.hs would be invalida as a regex since the * means "0 or more of the preceding character" and there is no preceding character there.
    – terdon
    Commented Aug 12, 2022 at 14:51
  • It doesn't work! How strange Commented Aug 12, 2022 at 14:54
  • 1
    Remember that this is all handled internally by ls, it isn't using the shell so it makes perfect sense that ls has its own internal language. Oh, and although you probably don't need to export at all, you certainly don't need to do it on two lines and can just export LS_COLORS='*.hs=35:*.py=93:*.zip=31:*.tar=31:*.tar.gz=31'.
    – terdon
    Commented Aug 12, 2022 at 14:56

1 Answer 1

2

I am not sure, but I suspect that this isn't possible since this is all about the way that ls itself handles whatever value you store in LS_COLORS. So unless they have chosen to implement what you describe, it won't be possible and you will need to bite the bullet and write it all out. At least you can do it on one line instead of two:

export LS_COLORS='*.hs=35:*.py=93:*.zip=31:*.tar=31:*.tar.gz=31'

You can also avoid the need to type it all out by using a trick like this:

$ printf '*.%s=31:' {zip,tar,tar.gz,tgz,Z,xz,7z}
*.zip=31:*.tar=31:*.tar.gz=31:*.tgz=31:*.Z=31:*.xz=31:*.7z=31:

And then you just need to copy/paste it once.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .