0

I have a problem with the following rule. If I do:

eval `/opt/local/libexec/gnubin/dircolors ~/.dircolors`
export LS_COLORS="${LS_COLORS}*README=00;37;44"

Then, when creating a README file, then I will get:

README

But now, I would like to apply the rule and do the same for every filename beginning by README (like README_something, README_important).

for this, I tried to put:

 export LS_COLORS="${LS_COLORS}*README*=00;37;44"

but it is not displayed as above image (only white).

How to manage the wildcards with LS_COLORS (I am on MacOS Big Sur)?

Edit 1

Following user1934428's advice, I tried with:

export LS_COLORS="${LS_COLORS}:*README*=00;37;44"

Unfortunately, for example, a filename like README_important doesn't display like my image above when I apply the command "l" which is actually defined by:

alias l='grc -es --colour=auto ls --color -Gh -C -lrt'

Why isn't the syntax README accepted ? especially, the second star which is supposed to expand all the files named like README_something, README_anything... etc

Edit 2

Here is the value $LS_COLORS, once I open a new terminal:

$ echo $LS_COLORS
no=01;37:fi=01;37:di=32:ln=01;36:pi=40;33:so=01;35:bd=40;33;01:cd=40;33;01:ex=00;36:*~=05;31:*.mtxt=05;31:*.ndx=05;31:*.cmd=00;33:*.exe=00;33:*.com=00;33:*.btm=00;33:*.bat=00;33:*.txt=00;37:*.pdf=04;94:*.docx=00;91:*.doc=00;91:*.xlsx=00;91:*.xls=00;91:*.c=00;35:*.h=00;35:*.sh=00;36:*.py=00;36:*.cpp=00;35:*.pl=00;36:*.pm=00;35:*.cgi=00;35:*.java=00;35:*.html=00;35:*.tar=00;31:*.tgz=00;31:*.arj=00;31:*.taz=00;31:*.lzh=00;31:*.zip=00;31:*.z=00;31:*.Z=00;31:*.gz=00;31:*.jpg=01;35:*.jpeg=01;35:*.JPG=01;35:*.gif=01;35:*.GIF=01;35:*.bmp=01;35:*.BMP=01;35:*.xbm=01;35:*.ppm=01;35:*.xpm=01;35:*.tif=01;35:*.png=01;35:*README*=00;37;44

I have now only one ':' character.

But this doesn't solve the original issue, i.e the taking into account of the wildcard for README* filenames.

halfer
  • 19,824
  • 17
  • 99
  • 186

2 Answers2

2

I see several problems here:

You obvously assume that LS_COLOURS already has a value, because you expand it. However, entries in LS_COLOURS are separated by a colon, and you don't have one.

The other problem is that sequence matters: ls parses the entries one after the other, as soon as it finds a matching one, this is the colour it uses. Therefore, more specific entries should be at the start of the colour list.

UPDATE

I tried out various variations with my version of ls(GNU coreutils 8.26) and found that a wildcard pattern such as *README* would be ignored. The only kind of wildcard pattern is an asterisk at the start of the entry. Hence,

LS_COLORS='*README-txt=35;46'  /bin/ls --color=yes *README*

colorizes my file x-README-txt, but

LS_COLORS='*README*=35;46'  /bin/ls --color=yes *README*
LS_COLORS='*READ*-txt=35;46'  /bin/ls --color=yes *README*

don't. Also, my above remark about the sequence of entries was not correct. While sequence indeed does matter, entries further to the right take priority. Therefore, if you have

LS_COLORS='*txt=35,46:*t=32,44'  /bin/ls --color=yes *README*

the file x-README-txt would be colorized using 32,44.

user1934428
  • 19,864
  • 7
  • 42
  • 87
  • Thanks for your answer. Could you take a look please at my **EDIT** ? It might be du to the wildcards which are not taken into account when I set : `*README*=00;37;44` –  Mar 16 '21 at 11:03
  • @youpilat13 : Well, you have implemented only half of my advice (adding the colon). At the very least, you could post the actual value of LS_COLOURS, which is more important than the way you manipulate it. – user1934428 Mar 16 '21 at 11:59
  • Thanks, I have put the current value of LS_COLORS into my **EDIT 2**. Could you talke a look please ? there is now only one '`:`' character. –  Mar 22 '21 at 16:52
  • Aside from the fact that there is no `README` entry in the list you posted, I played around a bit with my version of `ls` and found that even with this entry present, it would not work. I will update my answer accordingly. – user1934428 Mar 23 '21 at 06:06
  • thanks for your support. I have not yet found the solution to make appear colorized files beginning by **README** and following any character under the form **README_** : maybe the command `grc` puts the mess in this story. I don't know what to do ... –  Mar 23 '21 at 13:03
  • @youpilat13 : I fear that GNU ls is not designed to do this. In theory it would be possible: `ls` would just have to interpret the pattern as a general Glob pattern. Perhaps you could suggest this as a feature request? – user1934428 Mar 23 '21 at 13:30
  • @youpilat13 : I just stumbled over something which seems to do exactly what you want: Install `exa` and use it instead of `ls`. On the bottom of [this](https://the.exa.website/docs/colour-themes) documentation page is described a case which is very close to what you want to do. – user1934428 Mar 23 '21 at 13:43
  • Thanks, I didn't know it. However, the combination `exa -lrt` is not possible unlikely to classical `ls -lrt`. –  Mar 23 '21 at 20:52
  • I've never tried `exa` myself because I'm using Cygwin, and they have not ported it yet, but it looks promising.Perhaps you could try it out and post here your experiences. – user1934428 Mar 24 '21 at 07:49
0

I would like to apply the rule and do the same for every filename beginning by README (like README_something, README_important).

That is not possible. Looking at the sources of GNU ls https://github.com/wertarbyte/coreutils/blob/master/src/ls.c#L4206 the filename is matched form the end until it matches the string with strncmp.

Also this handles empty LS_COLORS:

export LS_COLORS="*README=00;37;44${LS_COLORS:+:${LS_COLORS}}"
KamilCuk
  • 120,984
  • 8
  • 59
  • 111
  • Is it possible to recompile 'ls' command and others by applying a modification to get what I want ? –  Mar 17 '21 at 08:33
  • I don't understand the goal of setting your export : `export LS_COLORS="*README=00;37;44${LS_COLORS:+:${LS_COLORS}}"` Could you tell me more about this ? –  Mar 17 '21 at 08:36
  • `Is it possible to recompile 'ls'` that is always possible. `Could you tell me more about this ?` `export` exports the variable to the environment, so that that variable is visible in child processes. Which part exactly you do not understand? – KamilCuk Mar 17 '21 at 08:42
  • @KamiCuk. Sorry, I thought your command would solve my issue, i.e to apply the same foreground/background on my files beginning by `README*` . Could you indicate please the modifications to do in `ls.c` –  Mar 17 '21 at 11:32